<<

PowerShell Basic Cheat Sheet Variables Importing, Exporting, Converting Aliases for common commands $var = "string" Assign variable Export -CliXML Import -CliXML Gcm Get - $a,$b = 0 or $a,$b = 'a','b' Assign multiple variables ConvertTo -XML ConvertTo -HTML Foreach,% Foreach -Object PowerShell is a task based command line and . To run it, click , PowerShell, run PowerShell ISE $a,$b = $b,$a Flip variables Export -CSV Import -CSV Sort Sort -Object or PowerShell as Administrator. $var=[int]5 Strongly typed variable ConvertTo -CSV ConvertFrom -CSV Where,? Where -Object Commands are written in verb -noun form, and named parameters start with a dash. Diff,compare Compare -Object Assignment, Logical, Comparison Operators Flow Control Basics , , gci Get -ChildItem =,+=, -=,++, -- Assign values to variable If(){} Elseif(){ } Else{ } Cmdlet Commands built into shell written in . Gi Get -Item -and, -or, -not,! Connect expressions / statements while(){} Functions Commands written in PowerShell language ,,cpi Copy -Item -eq, -ne Equal, not equal For($i=0; $i -lt 10; $i++){} Parameter Argument to a Cmdlet/Function/Script ,,mi Move -Item -gt, -ge Greater than, greater than or equal Foreach($file in dir C: \){$file.name} Shortcut for a Cmdlet or Function , Remove -Item -lt, -le Less than, less than or equal 1..10 | foreach{$_} Scripts Text files with .ps1 extension Rni, Rename -Item - “Hi” -replace “H”, “” Applications Existing windows programs Ft -Table -match, -notmatch Regular expression match Comments, Escape Characters Pipelines | Pass objects Get - word | Stop -Process #Comment Comment Fl Format -List -like, -notlike Wildcard matching Ctrl+c Interrupt current command <#comment#> Multiline Comment Gcim Get -CimInstance -contains, -notcontains Check if value in array Left/right Navigate editing cursor "`"test`"" Escape char ` ,gc,type Get -Content -in, -notin Reverse of contains,notcontains. Ctrl+left/right Navigate a word a `t Tab Sc Set -Content Home / End Move to start / end of line Parameters `n New line h,,ghy Get -History Up/down Move up and down through history -Confirm Prompt whether to take action ` Line continue Ihy, Invoke -History Insert Toggles between insert/overwrite mode -WhatIf Displays what command would do Gp Get -ItemProperty F7 Command history in a window Sp Set -ItemProperty Cmdlets Arrays, Objects Tab / Shift -Tab Command line completion ,gl Get -Location Get -EventLog Get -WinEvent $arr = "a", "b" Array of strings Gm Get -Member Get -Date $arr = @() Empty array Sls Select -String Get -Command Get all commands Start - Compare -Object $arr[5] Sixth array element ,chdir,sl Set -Location Get -Command -Module RGHS Get all commands in RGHS module Start -Job Get -Credential $arr[ -3.. -1] Last three array elements , Clear -Host Get -Command Get -p* Get all commands starting with get -p Test -Connection New -PSSession $arr[1,4+6..9] Elements at index 1,4, 6 -9 Cmdlets Get -help get -process for command Test - Split -Path $arr[1] += 200 Add to array item value Set -Location Get -Process | Get -Member Get members of the object Get -ADUser Get -ADComputer $z = $arA + $arB Two arrays into single array Get -Content Get -Process| format -list -properties * Get -Process as list with all properties Get -History New -ISESnippet [pscustomobject]@{x=1;z=2} Create custom object Add -Content Get -WMIObject Get -CimInstance (Get -Date).Date Date of object Set -Content Writing output and reading input Scripts Out -File "This displays a string" String is written directly to output Set -ExecutionPolicy -ExecutionPolicy Bypass Set execution policy to allow all scripts Out -String Write -Host "color" -ForegroundColor Red -NoNewLine String with colors, no new line at end ." \\c-is -ts -91 \c$ \scripts \script.ps1 " Run Script.PS1 script in current Copy -Item $age = Read -host "Please enter your age" Set $age variable to input from user &" \\c-is -ts -91 \c$ \scripts \script.ps1" Run Script.PS1 script in script scope Remove -Item $pwd = Read -host "Please enter your password" -asSecureString Read in $pwd as secure string .\Script.ps1 Run Script.ps1 script in script scope Move -Item Clear -Host Clear console $profile Your personal profile that runs at launch Set -Item Example command: dir C: \users \example -recurse -File | ?{$_.LastWriteTime -gt [datetime]::Today} | Select LastWriteTime,CreationTime,Length,FullName | sort LastWriteTime -descending | ft -AutoSize New -Item This gets all files under C: \users \example, filters by lastwritetime today, only returns lastwritetime, creationtime, length and fullname, sorts by lastwritetim e and outputs results in an autosized table