#Runs a ping, no .\ need since its in the sys 32 folder which is part of the environment path.
ping 127.0.0.1
#A test app that is run from the local folder but must be prefixed with the .\ because the current folder is no in the environment path.
.\testapp.exe
#Runs Get-Process
$str =
"get-process"
Invoke-Expression $str
#runs ping on multiple machines
$scriptblock = {ping server3}
Invoke-Command -scriptblock $scriptblock -computername
"server1"
,
"server2"
#opens all PDFs in the current directory
Invoke-Item *.pdf
&
'C:\Program Files\Windows Media Player\wmplayer.exe'
"c:\videos\my home video.avi"
/fullscreen
Things can get tricky when an external command has a lot of parameters or there are spaces in the arguments or paths! With spaces you have to nest Quotation marks and the result it is not always clear! In this case it is better to separate everything like so:
$CMD
=
'SuperApp.exe'
$arg1
'filename1'
$arg2
'-someswitch'
$arg3
'C:\documents and settings\user\desktop\some other file.txt'
$arg4
'-yetanotherswitch'
& $CMD $arg1 $arg2 $arg3 $arg4
# or same like that:
$AllArgs
@(
)
#runs DIR from a cmd shell, DIR in PowerShell is an alias to GCI. This will return the directory listing as a string but returns much faster than a GCI
cmd /c dir c:\windows
#starts a process, waits for it to finish and then checks the exit code.
$p = Start-Process ping -ArgumentList
"invalidhost"
-wait -NoNewWindow -PassThru
$p.HasExited
$p.ExitCode #to find available Verbs use the following code. $startExe = new-object System.Diagnostics.ProcessStartInfo -args PowerShell.exe $startExe.verbs
#runs Notepad.exe using the Static Start method and opens a file test.txt
[Diagnostics.Process]::Start(
"notepad.exe"
"test.txt"
$ps =
new
-
object
System.Diagnostics.Process
$ps.StartInfo.Filename =
"ipconfig.exe"
$ps.StartInfo.Arguments =
" /all"
$ps.StartInfo.RedirectStandardOutput = $True
$ps.StartInfo.UseShellExecute = $
false
$ps.start()
$ps.WaitForExit()
[
string
] $Out = $ps.StandardOutput.ReadToEnd();
MSDN Why: The Create() method for the Win32_Process class can be run locally or remotely over RPC rather than WSMAN (Invoke-Command), to spawn a process and returns a System.Management.ManagementBaseObject#\__PARAMETERS object that lists the process id and return code of the process start up. Details: This is a method in the Win32_Process class that allows creation of a process on a local or remote computer. There are multiple ways to use the Create() method. You can also provide Process Startup configuration data using the Win32_ProcessStartup class Examples:
#Opens a notepad process using Invoke-WMIMethod
Invoke-WMIMethod -Class Win32_Process -Name Create -ArgumentList Notepad.exe
#Opens a batch file process on Remote Computer using Invoke-WMIMethod
Invoke-WMIMethod -Class Win32_Process -Name Create -Computername RemoteServer -ArgumentList
"cmd /c C:\test.bat"
#Opens a notepad process using [wmiclass] accelerator
([wmiclass]
"win32_Process"
).create(
'notepad.exe'
#Opens a notepad process with process startup configuration to hide the window using [wmiclass] accelerator
$startup=[wmiclass]
"Win32_ProcessStartup"
$startup.Properties[
'ShowWindow'
].value=$False
'C:\'
,$Startup)
#Opens a batch file process using [wmiclass] accelerator on remote system
"\\remoteserver\root\cimv2:win32_Process"
'cmd /c C:\test.bat'
#Typical Return object showing processid and returnvalue
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ProcessId : 2708
ReturnValue : 0
Return Value Table
Successful Completion
Access Denied
Insufficient Privilege
Unknown failure
Path Not Found
Invalid Parameter
# icacls in V2
# You must use escape characters to prevent PowerShell from misinterpreting the parentheses.
icacls X:\VMS /grant Dom\HVAdmin:`(CI`)`(OI`)F
# In V3 you can use the stop-parsing symbol.
icacls X:\VMS --% /grant Dom\HVAdmin:(CI)(OI)F
Ed Price - MSFT edited Revision 37. Comment: Added Additional Resources
Carsten Siemens edited Revision 35. Comment: fixed typo
Richard Mueller edited Revision 34. Comment: Replace RGB values with color names in HTML to restore colors
Richard Mueller edited Revision 32. Comment: Fix duplicate <a name> tags in HTML
jrich edited Revision 31. Comment: cleaned up the --% section to match formatting
jrich edited Revision 30. Comment: dot sourcing correction, its not dot sourcing! (thanks lmat)
jrich edited Revision 26. Comment: minor changes and some more details added.
jrich edited Revision 25. Comment: minor wording change
Thomas Lee edited Revision 24. Comment: Minor grammar improvements
Ed Price - MSFT edited Revision 23. Comment: Featuring this article. Congrats!
FZB edited Revision 4. Comment: formating
jrich edited Revision 6. Comment: updated the call operator
Good and clear work! Thank you for that !
Yes, the sky is open for processing, thank you.
Craig Lussier edited Revision 8. Comment: added en-US to tags
Glad you guys like it. Im considering changing it a bit to not only talk about executables but include execution of cmdlets/scripts/function etc and provide more details about the call op. just need to change the title to something more descriptive.
Diagnostics.Process is more than that the static Start() method.
For example:
$ps = new-object System.Diagnostics.Process
$ps.StartInfo.Filename = "ipconfig.exe"
$ps.StartInfo.Arguments = " /all"
$ps.StartInfo.UseShellExecute = $false
[string] $Out = $ps.StandardOutput.ReadToEnd();
You could also $StartInfo = New-Object System.Diagnostics.ProcessStartInfo; the and call it with [Diagnostics.Process]::Start($startInfo)
$ps is the same as $p in $p = Start-Process ping -ArgumentList "invalidhost" -wait -NoNewWindow -PassThru
But from the Diagnostics.ProcessStartInfo object you can set redirect and UseShell Execute to save out to a string, and not just a file as with the PS cmdlet. you can also set event handlers and send input data to the application.
You can also build events like
$ps.Exited= [System.EventHandler] { Send-Mail -Body "It finished" } # Before .Start()
You could even turn on RedirectStandardInput and drive the application, though I have not had good luck with that.
Thanks for the article, I will bookmark this, is a mess trying to remember the differences, I always have to search my existing scripts.
- Josh
Josh, Thanks. I had read and knew a lot of that from the MSDN docs, just havent had time to populate it. Still a long way to go with this article!
I understand, just thought I'd chime in. I actually clarified some things when I went to proof that out.
Great, useful wiki. I read through the thread about it as well. Good to see all the information in one place now.
Boe Prox edited Revision 11. Comment: Added WMI Create() method to wiki