powershell: script to start a program with parameters?

resolver101 picture resolver101 · Jan 9, 2012 · Viewed 17.5k times · Source

When i run the Powershell script below i receive the error below. How do i run programs through powershell with parameters? The script will be a group policy logon.

Invoke-Expression : A positional parameter cannot be found that accepts argument '\TBHSERVER\NETLOGON\BGInfo\BGIFILE.bgi /timer:0 /s ilent /nolicprompt '. At X:\Systems\scripts\PowerShell\UpdateDesktopWithBGInfo.ps1:6 char:18 + Invoke-Expression <<<< $logonpath $ArguList + CategoryInfo : InvalidArgument: (:) [Invoke-Expression], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand

$LogonPath = $env:LOGONSERVER + "\NETLOGON\BGInfo\Bginfo.exe" 
$ArguList = $env:LOGONSERVER + '\NETLOGON\BGInfo\BGIFILE.bgi /timer:0 /silent /nolicprompt '
invoke-command $LogonPath
Invoke-Expression $logonpath $ArguList

Answer

Andy Arismendi picture Andy Arismendi · Jan 9, 2012

Invoke-Command is best suited for running commands remotely. As Shay points out you can use the ampersand & to tell PowerShell to execute something locally just like the cmd.exe shell.

In order to make Invoke-Command work you would need to do something like this:

$program = "C:\windows\system32\ping.exe"
$programArgs = "localhost", "-n", 1
Invoke-Command -ScriptBlock { & $program $programArgs }

Notice the use of the the ampersand in the script block. So if you are running a command locally just use the ampersand as Shay's example shows.