Powershell basics
From wikinotes
Concepts
- pipes/returns objects instead of text-streams
- cmdlet commands take the formate of
<verb>-<noun>
ex:Get-Command
.- case-insensitive (if do not like camelcasing, ignore it)
- windows is incapable of something like sudo or su. The cmd/powershell process either has admin privileges or does not. You can spawn a new powershell with admin rights using:
Start-Process powershell -Verb runas
Finding Help
There are two variants of the powershell.
- powershell (normal shell)
- powershell ISE (shell/interactive environment)
get-help <cmdlet-name> # display installed help get-help <cmdlet-name> -online # display help from online get-help Write-* # help for all cmdlets matching glob-match Get-Command -? # '-?' == 'Get-Help <cmdlet>' Get-Command <cmdlet-match>* # search for a specific command Get-Command -CommandType <Alias|Function|...> # restrict search for commands
invocation
This is really important . You cannot directly call a powershell script that contains a space, even if it is in quotes. The method of doing this is different depending on if you are running from a cmd.exe prompt, or a powershell.exe prompt.
# powershell powershell &'C:\New Folder\myscript.ps1'REM batch powershell -File 'C:\New Folder\myscript.ps1'
comments
test # This is an inline comment <# multiline comment #>
variable manipulation
$cwd = get-location # save output of cmdlet to variable $cwd # print variable $cwd | get-member # list object attributes $cwd | select-member -property * # list attributes/values $cwd.GetType() # same as $cwd | get-member
CmdLets
powershell objects are referred to as cmdlets. These builtin cmdlets are designed to interface with the operating system. Some examples:
man pages
Get-Help <cmdlet-name or command>filesystem
Get-Disk Get-Location Get-ChildItem ## like unix findexecution policy (run permissions)
Set-ExecutionPolicy unrestricted # change what ps scripts are allowed to run Get-ExecutionPolicydatetime
Get-Date Get-TimeZonesymbolic links
NOTE:
Non-Admin powershells do not have sufficient permissions to create symlinks. To enable this set of permissions, you'll need to update your group policy, then logout/login for the change to take effect.
Start Menu > gpedit.msc: - Computer configuration > Security Settings > Local Policies > User Rights Assignment: - Create Symbolic Links > (double click): - Add User or Group: <your username>New-Item -Path C:/dest -ItemType SymbolicLink -Value C:/srcProcess Management
Get-Process # list all running processes Get-Service # list all system services Get-EventLog # Stop-Process # kill a processRegistry
Get-ItemProperty -Path 'Registry::HKCU\Environment' Set-ItemProperty -PATH 'Registry::HKCU\Environment' -Value 'new;path;value' # checking for/adding to PATH $PATH = Get-ItemProperty -Path 'Registry::HKCU\Environment' -Name PATH if (!( $PATH.Path.Split(';').Contains( $rez_PATH ) )){ Set-ItemProperty -Path 'Registry::HKCU\Environment' -Name PATH -Value ($PATH.Path +";"+ $rez_PATH) }= Configuration = <blockquote> </blockquote><!-- Configuration -->Downloading
$client = New-Object System.Net.WebClient $client.DownloadFile("https://bootstrap.pypa.io/ez_setup.py", "ez_setup.py")