Running PS1 script using a batch file (.bat)

Currently, the following is my way to run the VMware vSphere PowerCLI command line. I want to run my sample.ps1 script automatically using a batch file. How can I put sample.ps1 in this path and create a batch file?

 C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\"" 
+4
source share
4 answers

If you are using PowerShell 2.0, you can use the -file for PowerShell.exe

 C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -file "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" 

If you are using PowerShell 1.0, you can use the -command parameter this way

 C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -command "& 'C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'" 
+3
source
 echo off Title,Report Script &color 9e for /f "usebackq delims=$" %%a in (`cd`) do ( set SCRIPTDIR=%%a ) (Set ScriptFile=%SCRIPTDIR%\Report.ps1) C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\";%ScriptFile%" 
+1
source

You can use this to run arbitrary .ps1 scripts through .bat files by calling the bat file like your ps1. Then extract the file name in batch mode and call powershell with it.

For a ready-to-use solution, use the following Gist: https://gist.github.com/JonasGroeger/10417237

0
source

I saw this code on another page, I am testing it in W2012 R2 and running it.

I hope this works:

 C:\>powershell "C:\>1\file.ps1" 
0
source

All Articles