I have a number of scenarios in which I need to pass parameters to command line exes.
I\'ve seen a number of these answered to some extent on this site, but so far I
Just pass parameters like you would in your BAT file.
cd "$($env:ProgramFiles(x86))\Microsoft Visual Studio 10.0\Common7\IDE"
devenv /command "File.BatchNewTeamProject C:\stuff\Project51.xml"
That will pass two arguments to devenv.
Now, I have run into a few cases where an application requires that quotes be in the string you pass it. I had some trouble with this and thought that PowerShell was dropping the quotes. But it turns out that the real problem is that PowerShell does not escape the quotes. So you need to do this.
devenv /command '\"File.BatchNewTeamProject C:\stuff\Project51.xml\"'
Try this function coming from PowerShell.com PowerTip it illustrate the usage of Invoke-Expression
.
function Call {
$command = $Args -join " "
$command += " 2>&1"
$result = Invoke-Expression($command)
$result |
%{$e=""}{ if( $_.WriteErrorStream ) {$e += $_ } else {$_} }{Write-Warning $e}
}
That gives :
cd "${env:ProgramFiles(x86)}\Microsoft Visual Studio 10.0\Common7\IDE"
call .\devenv.exe /command "`"File.BatchNewTeamProject C:\stuff\Project51.xml`"
--- Edit ---
There are many things to say here.
First you can find a good help with "about" files try :
Get-help about-*
On the subject you are interested you've got:
Get-help about_Quoting_Rules
Get-Help about_Special_Characters
Get-Help about_Escape_Characters
Get-Help about_Parameters
Second CD, DIR, MD
works, but they are just aliases on CmdLets which takes different arguments.
Third to get environment variable it's no longer %systemroot%
it's $env:systemroot
.
Fourth to start an executable file from powershell you can just type the name of the exe :
PS> notepad c:\temp\test.txt
The command line is first interpreted by powerShell so now if you write :
PS> "C:\Windows\System32\notepad.exe"
C:\Windows\System32\notepad.exe
It just interpret it as a string. So you can use the & operator and write
PS> & "C:\Windows\System32\notepad.exe" c:\test.txt
It works but :
PS> $a = "C:\Windows\System32\notepad.exe c:\test.txt"
PS> & $a
Fails and
PS> $a = "C:\Windows\System32\notepad.exe c:\test.txt"
PS> Invoke-Expression $a
Works