################################################### # Flow Control Statements ################################################### if (1 -eq 1) { } # Do something if 1 is equal to 1 do { 'hi' } while ($false) # Loop while a condition is true (always executes at least once) while ($false) { 'hi' } # While loops are not guaranteed to run at least once while ($true) { } # Do something indefinitely while ($true) { if (1 -eq 1) { break } } # Break out of an infinite while loop conditionally for ($i = 0; $i -le 10; $i++) { Write-Host $i } # Iterate using a for..loop foreach ($item in (Get-Process)) { } # Iterate over items in an array ################################################### # Variables ################################################### $a = 0 # Initialize a variable [int] $a = 'Trevor' # Initialize a variable, with the specified type (throws an exception) [string] $a = 'Trevor' # Initialize a variable, with the specified type (doesn't throw an exception) Get-Command -Name *varia* # Get a list of commands related to variable management Get-Variable # Get an array of objects, representing the variables in the current and parent scopes Get-Variable | ? { $PSItem.Options -contains 'constant' } # Get variables with the "Constant" option set Get-Variable | ? { $PSItem.Options -contains 'readonly' } # Get variables with the "ReadOnly" option set New-Variable -Name FirstName -Value Trevor New-Variable FirstName -Value Trevor -Option Constant # Create a constant variable, that can only be removed by restarting PowerShell New-Variable FirstName -Value Trevor -Option ReadOnly # Create a variable that can only be removed by specifying the -Force parameter on Remove-Variable Remove-Variable -Name firstname # Remove a variable, with the specified name Remove-Variable -Name firstname -Force # Remove a variable, with the specified name, that has the "ReadOnly" option set ################################################### # Functions ################################################### # A PowerShell Advanced Function, with all three blocks declared: BEGIN, PROCESS, END function Do-Something { [CmdletBinding]()] param () begin { } process { } end { } } ################################################### # Modules ################################################### Get-Command -Module PowerShellGet # Explore commands to manage PowerShell modules Find-Module -Tag cloud # Find modules in the PowerShell Gallery with a "cloud" tag Find-Module -Name ps* # Find modules in the PowerShell Gallery whose name starts with "PS" Install-Module -Name NameIT -Scope CurrentUser -Force # Install a module to your personal directory (non-admin) Install-Module -Name NameIT -Force # Install a module to your personal directory (admin / root) Install-Module -Name NameIT -RequiredVersion 1.9.0 # Install a specific version of a module Uninstall-Module -Name NameIT # Uninstall module called "NameIT", only if it was installed via Install-Module Register-PSRepository -Name -SourceLocation # Configure a private PowerShell module registry Unregister-PSRepository -Name -SourceLocation uri # Deregister a PowerShell Repository