#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
jrich edited Revision 15. Comment: commented that win32_process remotely works over RPC rather than wsman which all other methods use. also added object based process start example
Peter Kriegel edited Revision 18. Comment: 5. The Call Operator & Added Example for large amount of Parameters or spaces in Parameters
Nominating this article to be featured.
Thanks Ed!
wow.. thanks..!!
Peter Kriegel edited Revision 28. Comment: Added PowerShell 3.0 smart parsing advice, in & call operator section