Windows/PowerShell
Windows PowerShell
Windows PowerShell - http://www.microsoft.com/powershell
Wikipedia - Windows PowerShell
Windows PowerShell - Wikipedia - http://en.wikipedia.org/wiki/Windows_PowerShell
Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework. PowerShell provides full access to COM and WMI, enabling administrators to perform administrative tasks on both local and remote Windows systems.
In PowerShell, administrative tasks are generally performed by cmdlets (pronounced command-lets), specialized .NET classes implementing a particular operation. Sets of cmdlets may be combined together in scripts, executables (which are standalone applications), or by instantiating regular .NET classes (or WMI/COM Objects). These work by accessing data in different data stores, like the filesystem or registry, which are made available to the PowerShell runtime via Windows PowerShell providers.
Windows PowerShell also provides a hosting mechanism with which the Windows PowerShell runtime can be embedded inside other applications. These applications then leverage Windows PowerShell functionality to implement certain operations, including those exposed via the graphical interface. This capability has been utilized by Microsoft Exchange Server 2007 to expose its management functionality as PowerShell cmdlets and providers and implement the graphical management tools as PowerShell hosts which invoke the necessary cmdlets. Other Microsoft applications including Microsoft SQL Server 2008 also expose their management interface via PowerShell cmdlets. With PowerShell, graphical interface-based management applications on Windows are layered on top of Windows PowerShell. A PowerShell scripting interface for Windows products is mandated by the Common Engineering Criteria.
Windows PowerShell includes its own extensive, console-based help, similar to man pages in Unix shells via the Get-Help cmdlet
PowerShell 7
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
Migrating from Windows PowerShell 5.1 to PowerShell 7 https://learn.microsoft.com/en-us/powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7?view=powershell-7.4
Designed for cloud, on-premises, and hybrid environments, PowerShell 7 is packed with enhancements and new features.
- Installs and runs side-by-side with Windows PowerShell
- Improved compatibility with existing Windows PowerShell modules
- New language features, like ternary operators and ForEach-Object -Parallel
- Improved performance
- SSH-based remoting
- Cross-platform interoperability
- Support for Docker containers
PowerShell 7 Coexistance
PowerShell 7 is designed to coexist with Windows PowerShell 5.1. The following features ensure that your investment in PowerShell is protected and your migration to PowerShell 7 is simple. [1]
- Separate installation path and executable name
- Separate PSModulePath
- Separate profiles for each version
- Improved module compatibility
- New remoting endpoints
- Group policy support
- Separate Event logs
Install locations by version: [2]
- Windows PowerShell 5.1: $env:WINDIR\System32\WindowsPowerShell\v1.0
- PowerShell 6.x: $env:ProgramFiles\PowerShell\6
- PowerShell 7: $env:ProgramFiles\PowerShell\7
Install PowerShell 7
7.4.6:
7.4:
- Deploy PowerShell using the MSI package - https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.4#msi
- Deploy PowerShell using the ZIP package - https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.4#zip
The MSI package can be deployed and updated with management products such as Microsoft Configuration Manager. Download the packages from GitHub Release page.
- Microsoft Configuration Manager - https://learn.microsoft.com/en-us/configmgr/apps/
- GitHub Release page - https://github.com/PowerShell/PowerShell/releases
PowerShell 5
Included with Windows 10
PowerShell 2
Windows 7
Install PowerShell 2
Download Windows PowerShell 2.0 - http://support.microsoft.com/kb/968929
- Download the Windows Management Framework Core (WinRM 2.0 and Windows PowerShell 2.0)
- Install Windows PowerShell 2.0 - http://msdn.microsoft.com/en-us/library/ff637750.aspx
OLD: How to Download Windows PowerShell 1.0 - http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx
Editor
From PowerShell 1.0:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell_ise.exe
Windows PowerShell Quick Reference
A two-page quick (.doc) reference guide to using Windows PowerShell.
TechNet Scripting with Windows PowerShell
This page brings together resources for system administrators who are interested in learning about the Windows PowerShell command line and scripting environment.
Comparison of cmdlets with similar commands
The following table contains a selection of the cmdlets that ship with PowerShell noting the most similar commands in other well known command line interpreters.
Windows PowerShell (Cmdlet) |
Windows PowerShell (Alias) |
cmd.exe / COMMAND.COM (MS-DOS, Windows, OS/2, etc.) |
Bash (Unix, BSD, Linux, Mac OS X etc.) |
Description |
---|---|---|---|---|
Get-ChildItem | gci, dir, ls | dir | ls | List all files / directories in the (current) directory |
Get-Content | gc, type, cat | type | cat | Get the content of a file |
Get-Command | gcm | help | which | List available commands |
Get-Help | help, man | help | man | Help on commands |
Clear-Host | cls, clear | cls | clear | Clear the screen |
Copy-Item | cpi, copy, cp | copy | cp | Copy one or several files / a whole directory tree |
Move-Item | mi, move, mv | move | mv | Move a file / a directory to a new location |
Remove-Item | ri, del, erase, rmdir, rd, rm | del, erase, rmdir, rd | rm, rmdir | Delete a file / a directory |
Rename-Item | rni, ren, mv | ren, rename | mv | Rename a file / a directory |
Get-Location | gl, pwd | cd | pwd | Display the current directory/present working directory. |
Pop-Location | popd | popd | popd | Change the current directory to the directory most recently pushed onto the stack |
Push-Location | pushd | pushd | pushd | Push the current directory onto the stack |
Set-Location | sl, cd, chdir | cd, chdir | cd | Change the current directory |
Tee-Object | tee | n/a | tee | Pipe input to a file or variable, then pass the input along the pipeline |
Write-Output | echo, write | echo | echo | Print strings, variables etc. to standard output |
Get-Process | gps, ps | tlist, tasklist | ps | List all currently running processes |
Stop-Process | spps, kill | kill, taskkill | kill | Stop a running process |
Select-String | n/a | find, findstr | grep | Print lines matching a pattern |
Set-Variable | sv, set | set | set | Set the value of a variable / create a variable |
Security
PowerShell Cookbook - Get rid of the “execution of scripts is disabled” message - http://www.johndcook.com/PowerShellCookbook.html#a4
PSH> .\myScript.ps1 File D:\myScript.ps1 cannot be loaded. The file D:\myScript.ps1 is not digitally signed. The script will not execute on the system. Please see "get-help about_signing" for more details.. At line:1 char:13 + .\myScript.ps1 <<<<
PSH> set-executionPolicy RemoteSigned
Better option: (you will get a confirmation warning with this mode)
PSH> set-executionPolicy Unrestricted
To really unsecure the system:
PSH> set-executionPolicy ByPass
Run script in ByPass mode:
powershell -ExecutionPolicy ByPass -File script.ps1
WARNING: This has to be set for each environment separately. (32bit vs 64bit)
References and Tutorials
- Windows PowerShell - http://technet.microsoft.com/en-us/library/ee692944.aspx
- PowerShell.nu » Running Scripts with arguments in PowerShell - http://www.powershell.nu/2009/12/16/running-scripts-with-arguments-in-powershell/
- Powershell Beginner Tutorial « Tome's Land of IT - http://powertoe.wordpress.com/category/powershell/powershell-beginner-tutorial/
Code
Help
To see the examples, type: "get-help Get-Random -examples". For more information, type: "get-help Get-Random -detailed". For technical information, type: "get-help Get-Random -full".
Version
To see the version of powershell use $host.version
Windows 10:
C:\> powershell Windows PowerShell PS C:\> $host.version Major Minor Build Revision ----- ----- ----- -------- 5 1 22621 4111
Powershell Core: (pwsh)
C:\> pwsh PowerShell 7.4.5 PS C:\> $host.version Major Minor Build Revision ----- ----- ----- -------- 7 4 5 -1
---
Windows 7:
PS> $Host.Version Major Minor Build Revision ----- ----- ----- -------- 2 0 -1 -1
comments
# this is a comment echo "hello" # this is a comment
echo
echo "Hello $name" echo "Hello xxx$($name)xxx" Write-Output "Hello $name" "Hello ${name}" # same effect Write-Output -NoNewline "no newline"
$a = "Hello " $b = "World!" echo ($a + $b)
Write-Host -ForegroundColor green "Message" # write message in color
Path
echo $env:Path $env:Path = "${env:Path};C:\Python27;C:\Python27\Scripts"
Wrap Large Commands
With backticks:
some.exe ` --param1 ` --param2
Variables
$joe = 5 $joe = "abc" echo $joe echo --${joe}--
Get variable type:
$var.GetType().Name $var.GetType().FullName
Variable set:
if ( !$var ) { $var = "new-value"; } if ( $var -eq $null ) { $var = "new-value"; }
Arguments
"Arguments: $($args.count)" $args # list all args
$args[0] # is first argument arg1 = $args[0] arg2 = $args[1] echo "$arg1 $arg2"
param($Argument1,$Argument2) "Argument1 is: $Argument1" "Argument2 is: $Argument2"
Strings
Check for empty string:
if ($str) { "Not empty"}
Join operation: [1]
$a = "This", "Is", "a", "cat" # Operator join # This Is a cat $a -join ' ' # ThisIsacat -join $a # using conversion to [string] (and optionally use the separator $ofs) # This Is a cat [string]$a # This-Is-a-cat $ofs = '-' # ! after that all casts work in this way until $ofs changes ! [string]$a
$var.tostring() [string]$var1 = 5 # cast to string
Trim:
$msg = " Hello world " $msg.Trim()
Comparison:
if ( $str1 -eq $str2 ) { ... } if ( $str1.startswith('something') ) { ... }
Variable Expansion:
"This is expansion: $($obj.Name)"
Exit Code
exit 1
If
PowerShell Tutorial - Conditional Logic | PowerShell Pro! - http://www.powershellpro.com/powershell-tutorial-introduction/powershell-tutorial-conditional-logic/
if (! $args[0]) { "Error: missing parameter" exit 1 } if ($args[1]) { $name = $args[1] }
Else:
if (...) {} else {}
Comparision:
-eq -ne -lt -gt -le -ge if ($name -eq "joe") { ... } if ($code -ne 0) { ... } if ($rc -eq $true) { ... }
Boolean Logic:
-and -or
Null:
if ($myvar -eq $null) { ... }
Return Codes
Return codes:
# True - on success # False - on fail echo $?
if ($?) { ... } if (!$?) { ... }
Examples:
true echo $? # True
false echo $? # False
echo "hi" echo $? # True
echasdfasdfasdf echo $? # False
Dates
$Year = Read-Host "When were you born?" $Numerical_Years = (Get-Date -uformat "%Y") -$Year
Select Columns
Select all columns:
pwd | select *
Select drive:
pwd | select Drive
Loops
for
for ($i=1; $i -lt 6; $i++) { "This is line number " + $i }
Break Continue
break # <- abort loop #continue # <- skip just this iteration, but continue loop #return # <- abort code, and continue in caller scope #exit # <- abort code at caller scope
Ref[2]
foreach
foreach ($item in $items) { $item }
while
while ($true) { ... }
do while
do { ... } While ($a -le 5)
do { sleep 2 ping google.com -n 1 } while ( ! $? )
Aliases
To see a list of all the cmdlet aliases:
get-alias
Booleans
$true # echo as 'True' $false # echo as 'False'
if ($rc -eq $true) { ... } if ($rc -eq $false) { ... } if ($rc -ne $true) { ... }
Arrays
a.k.a. Lists
$empty_array = @() # does not actually appear to create an object $items = 1,"Hello",3.5 $items = @(1,"Hello",3.5) # explicit syntax $item1,$item2,$items_remaining = $items # distribute values $numbers = 1,2,3,4,5,6,7 $numbers = (1..7) $items # list all items $var = $items[0] # first element $vars = $items[2..3] # items from index 2 to 3 $vars = $items[-1] # last element $item[2] = 64 # modify value (index must exist) $item.SetValue(2, 64) # modify value (index must exist) $items += 25 # append new value $new_array = $first_array + $second_array # append arrays $count = $items.length-1 foreach ($item in $items) {$item} # loop through array $items.gettype() # property BaseType will show System.Array
$array = @("test1", "test2", "test3") for ($i=0; $i -lt $array.length; $i++) { $array[$i] } foreach ($element in $array) { $element } $array |foreach { $_ }
Referneces:
- Powershell Arrays | SS64.com - http://ss64.com/ps/syntax-arrays.html
Hash Tables
a.k.a. Dictionaries
$states = @{"Washington" = "Olympia"; "Oregon" = "Salem"; California = "Sacramento"} $states # list states $states.Add("Alaska", "Fairbanks") $states["Alaska"] = "Fairbanks" # same effect $states.Remove("Alaska") $states.Set_Item("Alaska", "Juneau") $states.Get_Item("Oregon") $states.ContainsKey("Oregon") $states.ContainsValue("Salem") $states | Sort-Object $states.GetEnumerator() | Sort-Object Name $states.GetEnumerator() | Sort-Object Value -descending
References:
- Windows PowerShell Tip: Working with Hash Tables - http://technet.microsoft.com/en-us/library/ee692803.aspx
Splat Operator
$params = @{path = "c:\demo"; Recurse= $true} dir @params
That will in effect run: dir -Path c:\demo -Recurse:$true
Here Strings
@'...'@ and @"..."@ are "Here Strings"
$myHereString = @' some text with "quotes" and variable names $printthis some more text '@
ref
Function
Function Add { param($x, $y) $ans = $x + $y } Function Add { "params: $args" } Function Add { $args[0] + $args[1] } Function Age { Param([int]$x) } # type casting
Objects
pwd (alias for Get-Location) returns an object describing the current working directory:
> pwd | select * PowerCLI C:\Windows\system32> pwd | select * Drive Provider ProviderPath Path ----- -------- ------------ ---- C Microsoft.PowerS... C:\Windows\system32 C:\Windows\system32
filter a column:
$obj | select [COLUMN]
Filter records (row): [4]
$obj | Where-Object {$_.Name -eq "Something"} $obj | Where-Object {$_.Name.StartsWith("something")}
See #Where-Object
To get a sub object:
$drive = (pwd).Drive pwd | Select-Object Drive
To get a particular property:
$drive = (pwd).Drive.Name # Get drive letter $path = pwd | select -exp Path
Get Object Members:
pwd | GetMember pwd | gm # equivalent
Record count:
($obj | measure-object).count # or $obj_measure = $obj | measure-object $obj_measure.count
Users
List local users:
Get-LocalUser
List local groups:
Get-LocalGroup
Environment Variables
List all:
Get-ChildItem Env:
Set:
[Environment]::SetEnvironmentVariable('Foo','Bar') [Environment]::GetEnvironmentVariable('Foo')
Remove:
[Environment]::SetEnvironmentVariable('Foo',) [Environment]::GetEnvironmentVariable('Foo')
Profile Enviornment Varialbes
You can get the path to your PowerShell profile with the $PROFILE automatic variable.
Available in any powershell that loads your profile:
$Env:CompanyUri = 'https://internal.contoso.com' $Env:Path += ';C:\Tools'
Persistent Environment Variables
Set:
[Environment]::SetEnvironmentVariable('Foo', 'Bar', 'Machine')
Delete:
[Environment]::SetEnvironmentVariable('Foo', , 'Machine')
Grep like filter
... | select-string -pattern "PATTERN"
... | select-string -pattern "PATTERN"
echo "hello" | select-string -pattern "ll" # ok echo "hello" | select-string -pattern "e..o" # ok echo "helllo" | select-string -pattern "e..o" # fail echo "hello" | select-string -pattern "e.+o" # ok echo "hello" | select-string -pattern "e.+t" # fail
echo "hello" | select-string "e.+o" # pass -- seems like "-pattern" is default!
help select-string The `Select-String` cmdlet uses regular expression matching to search for text patterns in input strings and files. You can use `Select-String` similar to `grep` in UNIX or `findstr.exe` in Windows.
Where-Object
Filter data with Where-Object [5]
-lt -- Less than -le -- Less than or equal to -gt -- Greater than -ge -- Greater than or equal to -eq -- Equal to -ne -- Not equal to -like - Like; uses wildcards for pattern matching
Where-Object {$_.Name -eq "Something"} Where-Object {$_.Name -like "*th*"}
Try Catch Exception Handling
try { # code throw 'Error' exit 0 } catch { # $_ will contain details echo "Exception: " $_ # will show 'Error' exit 1 # failure }
Stop on Error
To make powershell stop executing whenever it encounters error, set
$ErrorActionPreference = "Stop" # or Get-Process -ErrorAction Stop
Redirection
null:
# technically any non assigned variable will work the same echo "Hello world" > $null # redirect to like /dev/null
STDOUT:
Write-Output "Hello World" echo "Hello world" > $myfile
STDERR:
Write-Error "Some Error" Write-Error "Some Error" 2> $myfile Write-Error "Some Error" 2>&1
Write-Warning "Some Warning" Write-Warning "Hidden Warning" -WarningAction SilentlyContinue # supress warnings
Streams
Capture Warning, Verbose, Debug and Host Output via alternate streams: (available in PowerShell 3.x)
0 - stdin 1 - stdout (write-output, normal cmdlet output, non captured expression output) 2 - stderr (write-error, throw, cmdlet non-terminating errors) 3 - warnings (write-warning, cmdlet warning) 4 - verbose (write-verbose and cmdlet -verbose output) 5 - debug (write-debug and cmdlet debug output) 6 - host (write-host, read-host, explicit out-host use) 9 - combined (all output combined into a single - easy to redirect stream)
capture *ALL* output to a log file
MyLongRunningComplexUnattendedScript.ps1 9>&1 > LogFile.log
References:
- Capture Warning, Verbose, Debug and Host Output via alternate streams | Microsoft Connect - http://connect.microsoft.com/PowerShell/feedback/details/297055/capture-warning-verbose-debug-and-host-output-via-alternate-streams
Progress Bar
Write-Progress
for ($i = 1; $i -le 100; $i++ ){write-progress -activity "Search in Progress" -status "$i% Complete:" -percentcomplete $i;}
References:
- Technet Write-Progress - http://technet.microsoft.com/en-us/library/hh849902.aspx
Snapin
if ( (Get-PSSnapin -Name MySnapin -ErrorAction SilentlyContinue) -eq $null ) { Add-PsSnapin MySnapin }
References:
- How to check if PowerShell snap-in is already loaded before calling Add-PSSnapin - Stack Overflow - http://stackoverflow.com/questions/1477994/how-to-check-if-powershell-snap-in-is-already-loaded-before-calling-add-pssnapin
Cmdlets
Read-Host - Get Input From User
$name = Read-Host "name" # name:
Read-Host "Press enter key to continue"
$fname = [Console]::ReadLine()
Password:
$pass = Read-Host 'What is your password?' -AsSecureString $plan_text_pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto( [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))
Confirm:
start calc gps calc | stop-process -confirm # firm calc process, and confirm kill
Steal pop up dialog box from visual basic: [8] (did not work for me!)
$a = new-object -comobject MSScriptControl.ScriptControl $a.language = "vbscript" $a.addcode("function getInput() getInput = inputbox(`"Message box prompt`",`"Message Box Title`") end function" ) $b = $a.eval("getInput")
Input Box [9] (works):
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null $computer = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a computer name", "Computer", "$env:computername")
All user input and output normally occurs inside the PowerShell console. Simply access the .NET framework directly if you'd like to get back the same dialogs VBScript scripters are using. Here is the old-fashioned InputBox:
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') $name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter your name", "Name", "$env:username") "Your name is $name"
An Easy InputBox - And here is your MsgBox: [10]
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') $result = [Microsoft.VisualBasic.Interaction]::MsgBox( ` "Do you agree?", 'YesNoCancel,Question', "Respond please") switch ($result) { 'Yes' { "Ah good" } 'No' { "Sorry to hear that" } 'Cancel' { "Bye..." } }
Clear-Host - Clear Screen
Clear-Host
Get-Content - Get File Content
Get file content:
Get-Content [file]
Aliases:
- cat
- type
- gc
Get-Content dictionary.txt | foreach {Write-Output $_} Get-Content dictionary.txt | foreach {Write-Output $_.length} Get-Content dictionary.txt | foreach {Write-Output $_.toupper()} Get-Content dictionary.txt | foreach {$_.length} # Write-Output is implied. $content = Get-Content dictionary.txt (Get-Content test.txt)[3 .. 5] # get lines 3 through 5 of file foreach ($line in Get-Content test.txt) { echo $line } # read each line of a file
References:
- Using the Get-Content Cmdlet - http://technet.microsoft.com/en-us/library/ee176843.aspx
- Windows PowerShell Tip: Reading is Fundamental - http://technet.microsoft.com/en-us/library/ee692806.aspx]
Start-Sleep - Pause Script
Suspends the activity in a script or session for the specified period of time:
Start-Sleep [seconds]
Alias:
- sleep
Get-Random - Generate Random Numbers
Generate a number within the range of 100 to 200 (including both)
Get-Random -minimum 100 -maximum 200
Generate 10 numbers within the range of 1 to 100 (including 1 and 100). Numbers can only be used once, so if the count is higher than the InputObject range, you won't get the full count.
Get-Random -Count 10 -InputObject (1..100)
Scripts
Console Redirect
console.ps1
while ($true) { Read-Host }
Usage: (end with [ctrl]+[c])
./console.ps1 > file
Powershell Command Line Options
C:\> powershell.exe /? PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>] [-NoLogo] [-NoExit] [-Sta] [-NoProfile] [-NonInteractive] [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}] [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>] [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>] [-Command { - | <script-block> [-args <arg-array>] | <string> [<CommandParameters>] } ] PowerShell[.exe] -Help | -? | /? -PSConsoleFile Loads the specified Windows PowerShell console file. To create a console file, use Export-Console in Windows PowerShell. -Version Starts the specified version of Windows PowerShell. -NoLogo Hides the copyright banner at startup. -NoExit Does not exit after running startup commands. -Sta Start the shell using a single-threaded apartment. -NoProfile Does not use the user profile. -NonInteractive Does not present an interactive prompt to the user. -InputFormat Describes the format of data sent to Windows PowerShell. Valid values are "Text" (text strings) or "XML" (serialized CLIXML format). -OutputFormat Determines how output from Windows PowerShell is formatted. Valid values are "Text" (text strings) or "XML" (serialized CLIXML format). -WindowStyle Sets the window style to Normal, Minimized, Maximized or Hidden. -EncodedCommand Accepts a base-64-encoded string version of a command. Use this parameter to submit commands to Windows PowerShell that require complex quotation marks or curly braces. -File Execute a script file. -ExecutionPolicy Sets the default execution policy for the session. -Command Executes the specified commands (and any parameters) as though they were typed at the Windows PowerShell command prompt, and then exits, unless NoExit is specified. The value of Command can be "-", a string. or a script block. If the value of Command is "-", the command text is read from standard input. If the value of Command is a script block, the script block must be enclosed in braces ({}). You can specify a script block only when running PowerShell.exe in Windows PowerShell. The results of the script block are returned to the parent shell as deserialized XML objects, not live objects. If the value of Command is a string, Command must be the last parameter in the command , because any characters typed after the command are interpreted as the command arguments. To write a string that runs a Windows PowerShell command, use the format: "& {<command>}" where the quotation marks indicate a string and the invoke operator (&) causes the command to be executed. -Help, -?, /? Shows this message. If you are typing a PowerShell.exe command in Windows PowerShell, prepend the command parameters with a hyphen (-), not a forward slash (/). You can use either a hyphen or forward slash in Cmd.exe. EXAMPLES PowerShell -PSConsoleFile SqlSnapIn.Psc1 PowerShell -version 1.0 -NoLogo -InputFormat text -OutputFormat XML PowerShell -Command {Get-EventLog -LogName security} PowerShell -Command "& {Get-EventLog -LogName security}" # To use the -EncodedCommand parameter: $command = 'dir "c:\program files" ' $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) $encodedCommand = [Convert]::ToBase64String($bytes) powershell.exe -encodedCommand $encodedCommand
Cmdlets Aliases
PS C:\> Get-Alias CommandType Name Definition ----------- ---- ---------- Alias % ForEach-Object Alias ? Where-Object Alias ac Add-Content Alias asnp Add-PSSnapIn Alias cat Get-Content Alias cd Set-Location Alias chdir Set-Location Alias clc Clear-Content Alias clear Clear-Host Alias clhy Clear-History Alias cli Clear-Item Alias clp Clear-ItemProperty Alias cls Clear-Host Alias clv Clear-Variable Alias compare Compare-Object Alias copy Copy-Item Alias cp Copy-Item Alias cpi Copy-Item Alias cpp Copy-ItemProperty Alias cvpa Convert-Path Alias dbp Disable-PSBreakpoint Alias del Remove-Item Alias diff Compare-Object Alias dir Get-ChildItem Alias ebp Enable-PSBreakpoint Alias echo Write-Output Alias epal Export-Alias Alias epcsv Export-Csv Alias epsn Export-PSSession Alias erase Remove-Item Alias etsn Enter-PSSession Alias exsn Exit-PSSession Alias fc Format-Custom Alias fl Format-List Alias foreach ForEach-Object Alias ft Format-Table Alias fw Format-Wide Alias gal Get-Alias Alias gbp Get-PSBreakpoint Alias gc Get-Content Alias gci Get-ChildItem Alias gcm Get-Command Alias gcs Get-PSCallStack Alias gdr Get-PSDrive Alias ghy Get-History Alias gi Get-Item Alias gjb Get-Job Alias gl Get-Location Alias gm Get-Member Alias gmo Get-Module Alias gp Get-ItemProperty Alias gps Get-Process Alias group Group-Object Alias gsn Get-PSSession Alias gsnp Get-PSSnapIn Alias gsv Get-Service Alias gu Get-Unique Alias gv Get-Variable Alias gwmi Get-WmiObject Alias h Get-History Alias history Get-History Alias icm Invoke-Command Alias iex Invoke-Expression Alias ihy Invoke-History Alias ii Invoke-Item Alias ipal Import-Alias Alias ipcsv Import-Csv Alias ipmo Import-Module Alias ipsn Import-PSSession Alias ise powershell_ise.exe Alias iwmi Invoke-WMIMethod Alias kill Stop-Process Alias lp Out-Printer Alias ls Get-ChildItem Alias man help Alias md mkdir Alias measure Measure-Object Alias mi Move-Item Alias mount New-PSDrive Alias move Move-Item Alias mp Move-ItemProperty Alias mv Move-Item Alias nal New-Alias Alias ndr New-PSDrive Alias ni New-Item Alias nmo New-Module Alias nsn New-PSSession Alias nv New-Variable Alias ogv Out-GridView Alias oh Out-Host Alias popd Pop-Location Alias ps Get-Process Alias pushd Push-Location Alias pwd Get-Location Alias r Invoke-History Alias rbp Remove-PSBreakpoint Alias rcjb Receive-Job Alias rd Remove-Item Alias rdr Remove-PSDrive Alias ren Rename-Item Alias ri Remove-Item Alias rjb Remove-Job Alias rm Remove-Item Alias rmdir Remove-Item Alias rmo Remove-Module Alias rni Rename-Item Alias rnp Rename-ItemProperty Alias rp Remove-ItemProperty Alias rsn Remove-PSSession Alias rsnp Remove-PSSnapin Alias rv Remove-Variable Alias rvpa Resolve-Path Alias rwmi Remove-WMIObject Alias sajb Start-Job Alias sal Set-Alias Alias saps Start-Process Alias sasv Start-Service Alias sbp Set-PSBreakpoint Alias sc Set-Content Alias select Select-Object Alias set Set-Variable Alias si Set-Item Alias sl Set-Location Alias sleep Start-Sleep Alias sort Sort-Object Alias sp Set-ItemProperty Alias spjb Stop-Job Alias spps Stop-Process Alias spsv Stop-Service Alias start Start-Process Alias sv Set-Variable Alias swmi Set-WMIInstance Alias tee Tee-Object Alias type Get-Content Alias where Where-Object Alias wjb Wait-Job Alias write Write-Output
VMware PowerCLI
See VMware PowerCLI
Recipies
Loop till ping success
ping store.lab -n 1 while( ! $? ) { sleep 1 ping store.lab -n 1 }
Issues
Unable to Run Script
Permissions
keywords
- ↑ https://learn.microsoft.com/en-us/powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7?view=powershell-7.4
- ↑ https://learn.microsoft.com/en-us/powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7?view=powershell-7.4
- ↑ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.4