Skip to content

Instantly share code, notes, and snippets.

@aroyrev
Forked from pcgeek86/cheatsheet.ps1
Created July 4, 2019 06:22
Show Gist options
  • Save aroyrev/cc4bdb607e4f482466590e7927749576 to your computer and use it in GitHub Desktop.
Save aroyrev/cc4bdb607e4f482466590e7927749576 to your computer and use it in GitHub Desktop.
PowerShell Cheat Sheet / Quick Reference
###################################################
# Conditional Statements
###################################################
if (1 -eq 1) { } # Do something if 1 is equal to 1
$a = 0
###################################################
# Variables
###################################################
Get-Variable # Get an array of objects, representing the variables in the current scope
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
###################################################
# 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)
Uninstall-Module -Name NameIT # Uninstall module called "NameIT", only if it was installed via PowerShell Gallery
Register-PSRepository -Name <repo> -SourceLocation <uri> # Configure a private PowerShell module registry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment