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