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.

Revisions

  1. @pcgeek86 pcgeek86 revised this gist Feb 18, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ $a -gt 2 # Greater than compari
    $a -lt 3 # Less than comparison operator

    $FirstName = 'Trevor'
    $FirstName -like 'T*' # Use the -like operator, which supports the wildcard (*) character. Returns $true
    $FirstName -like 'T*' # Perform string comparison using the -like operator, which supports the wildcard (*) character. Returns $true

    ###################################################
    # Regular Expressions
  2. @pcgeek86 pcgeek86 revised this gist Feb 18, 2019. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,10 @@ $FirstName -like 'T*' # Use the -like operat
    # Regular Expressions
    ###################################################

    'Trevor' -match '^T\w*' # Perform a regular expression match against a string value. # Returns $true and populates $matches variable
    $matches[0] # Returns 'Trevor', based on the above match

    @('Trevor', 'Billy', 'Bobby') -match '^B' # Perform a regular expression match against an array of string values. Returns Billy, Bobby

    ###################################################
    # Flow Control
  3. @pcgeek86 pcgeek86 revised this gist Feb 18, 2019. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -15,12 +15,21 @@ Get-Help -Name Get-Command -Parameter Module # Get help for a speci

    $a = 2 # Basic variable assignment operator
    $a += 1 # Incremental assignment operator
    $a -= 1 # Decrement assignment operator

    $a -eq 0 # Equality comparison operator
    $a -ne 5 # Not-equal comparison operator
    $a -gt 2 # Greater than comparison operator
    $a -lt 3 # Less than comparison operator

    $FirstName = 'Trevor'
    $FirstName -like 'T*' # Use the -like operator, which supports the wildcard (*) character. Returns $true

    ###################################################
    # Regular Expressions
    ###################################################


    ###################################################
    # Flow Control
    ###################################################
  4. @pcgeek86 pcgeek86 revised this gist Feb 14, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -143,6 +143,8 @@ Remove-Item -Path testing.txt # Delete a file
    Get-CimInstance -ClassName Win32_BIOS # Retrieve BIOS information
    Get-CimInstance -ClassName Win32_DiskDrive # Retrieve information about locally connected physical disk devices
    Get-CimInstance -ClassName Win32_PhysicalMemory # Retrieve information about install physical memory (RAM)
    Get-CimInstance -ClassName Win32_NetworkAdapter # Retrieve information about installed network adapters (physical + virtual)
    Get-CimInstance -ClassName Win32_VideoController # Retrieve information about installed graphics / video card (GPU)

    Get-CimClass -Namespace root\cimv2 # Explore the various WMI classes available in the root\cimv2 namespace
    Get-CimInstance -Namespace root -ClassName __NAMESPACE # Explore the child WMI namespaces underneath the root\cimv2 namespace
  5. @pcgeek86 pcgeek86 revised this gist Feb 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -119,7 +119,7 @@ Install-Module -Name NameIT -RequiredVersion 1.9.0 # Install a specific
    Uninstall-Module -Name NameIT # Uninstall module called "NameIT", only if it was installed via Install-Module

    Register-PSRepository -Name <repo> -SourceLocation <uri> # Configure a private PowerShell module registry
    Unregister-PSRepository -Name <repo> -SourceLocation uri # Deregister a PowerShell Repository
    Unregister-PSRepository -Name <repo> # Deregister a PowerShell Repository


    ###################################################
  6. @pcgeek86 pcgeek86 revised this gist Feb 14, 2019. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -135,3 +135,15 @@ Set-Content -Path c:\test.txt -Value '' # Create an empty fi

    Remove-Item -Path testing.txt # Delete a file
    [System.IO.File]::Delete('testing.txt') # Delete a file using .NET Base Class Library

    ###################################################
    # Windows Management Instrumentation (WMI) (Windows only)
    ###################################################

    Get-CimInstance -ClassName Win32_BIOS # Retrieve BIOS information
    Get-CimInstance -ClassName Win32_DiskDrive # Retrieve information about locally connected physical disk devices
    Get-CimInstance -ClassName Win32_PhysicalMemory # Retrieve information about install physical memory (RAM)

    Get-CimClass -Namespace root\cimv2 # Explore the various WMI classes available in the root\cimv2 namespace
    Get-CimInstance -Namespace root -ClassName __NAMESPACE # Explore the child WMI namespaces underneath the root\cimv2 namespace

  7. @pcgeek86 pcgeek86 revised this gist Feb 14, 2019. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -119,4 +119,19 @@ Install-Module -Name NameIT -RequiredVersion 1.9.0 # Install a specific
    Uninstall-Module -Name NameIT # Uninstall module called "NameIT", only if it was installed via Install-Module

    Register-PSRepository -Name <repo> -SourceLocation <uri> # Configure a private PowerShell module registry
    Unregister-PSRepository -Name <repo> -SourceLocation uri # Deregister a PowerShell Repository
    Unregister-PSRepository -Name <repo> -SourceLocation uri # Deregister a PowerShell Repository


    ###################################################
    # Filesystem
    ###################################################

    New-Item -Path c:\test -ItemType Directory # Create a directory
    mkdir c:\test2 # Create a directory (short-hand)

    New-Item -Path c:\test\myrecipes.txt # Create an empty file
    Set-Content -Path c:\test.txt -Value '' # Create an empty file
    [System.IO.File]::WriteAllText('testing.txt', '') # Create an empty file using .NET Base Class Library

    Remove-Item -Path testing.txt # Delete a file
    [System.IO.File]::Delete('testing.txt') # Delete a file using .NET Base Class Library
  8. @pcgeek86 pcgeek86 revised this gist Feb 14, 2019. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,21 @@ Get-Help -Name about_Variables # Get help for a speci
    Get-Help -Name Get-Command # Get help for a specific PowerShell function
    Get-Help -Name Get-Command -Parameter Module # Get help for a specific parameter on a specific command


    ###################################################
    # Operators
    ###################################################

    $a = 2 # Basic variable assignment operator
    $a += 1 # Incremental assignment operator

    $a -eq 0 # Equality comparison operator
    $a -ne 5 # Not-equal comparison operator
    $a -gt 2 # Greater than comparison operator
    $a -lt 3 # Less than comparison operator

    ###################################################
    # Flow Control Statements
    # Flow Control
    ###################################################

    if (1 -eq 1) { } # Do something if 1 is equal to 1
  9. @pcgeek86 pcgeek86 revised this gist Feb 14, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -58,8 +58,9 @@ Remove-Variable -Name firstname -Force # Remove a variable, w
    # Functions
    ###################################################

    # A PowerShell Advanced Function, with all three blocks declared: BEGIN, PROCESS, END
    function Do-Something {
    function add ($a, $b) { $a + $b } # A basic PowerShell function

    function Do-Something { # A PowerShell Advanced Function, with all three blocks declared: BEGIN, PROCESS, END
    [CmdletBinding]()]
    param ()
    begin { }
  10. @pcgeek86 pcgeek86 revised this gist Feb 14, 2019. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,15 @@ while ($true) { if (1 -eq 1) { break } } # Break out of an infi
    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

    switch ('test') { 'test' { 'matched'; break } } # Use the switch statement to perform actions based on conditions. Returns string 'matched'
    switch -regex (@('Trevor', 'Daniel', 'Bobby')) { # Use the switch statement with regular expressions to match inputs
    'o' { $PSItem; break } # NOTE: $PSItem or $_ refers to the "current" item being matched in the array
    }
    switch -regex (@('Trevor', 'Daniel', 'Bobby')) { # Switch statement omitting the break statement. Inputs can be matched multiple times, in this scenario.
    'e' { $PSItem }
    'r' { $PSItem }
    }

    ###################################################
    # Variables
    ###################################################
  11. @pcgeek86 pcgeek86 revised this gist Feb 14, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    Get-Command # Retrieves a list of all the commands available to PowerShell
    # (native binaries in $env:PATH + cmdlets / functions from PowerShell modules)
    Get-Command -Module Microsoft* # Retrieves a list of all the PowerShell commands exported from modules named Microsoft*
    Get-Command -Name *item # Retrieves a list of all commands (native binaries + PowerShell commands) ending in "item"

    Get-Help # Get all help topics
    Get-Help -Name about_Variables # Get help for a specific about_* topic (aka. man page)
  12. @pcgeek86 pcgeek86 revised this gist Feb 13, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -65,6 +65,8 @@ Get-Command -Name *module* -Module mic*core # Which commands can
    Get-Module -ListAvailable # Show me all of the modules installed on my system (controlled by $env:PSModulePath)
    Get-Module # Show me all of the modules imported into the current session

    $PSModuleAutoLoadingPreference = 0 # Disable auto-loading of installed PowerShell modules, when a command is invoked

    Import-Module -Name NameIT # Explicitly import a module, from the specified filesystem path or name (must be present in $env:PSModulePath)
    Remove-Module -Name NameIT # Remove a module from the scope of the current PowerShell session

  13. @pcgeek86 pcgeek86 revised this gist Feb 13, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -65,8 +65,8 @@ Get-Command -Name *module* -Module mic*core # Which commands can
    Get-Module -ListAvailable # Show me all of the modules installed on my system (controlled by $env:PSModulePath)
    Get-Module # Show me all of the modules imported into the current session

    Import-Module # Explicitly import a module, from the specified filesystem path or name (must be present in $env:PSModulePath)
    Remove-Module # Remove a module from the scope of the current PowerShell session
    Import-Module -Name NameIT # Explicitly import a module, from the specified filesystem path or name (must be present in $env:PSModulePath)
    Remove-Module -Name NameIT # Remove a module from the scope of the current PowerShell session

    New-ModuleManifest # Helper function to create a new module manifest. You can create it by hand instead.

  14. @pcgeek86 pcgeek86 revised this gist Feb 13, 2019. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    Get-Command
    Get-Help
    Get-Command # Retrieves a list of all the commands available to PowerShell
    # (native binaries in $env:PATH + cmdlets / functions from PowerShell modules)

    Get-Help # Get all help topics
    Get-Help -Name about_Variables # Get help for a specific about_* topic (aka. man page)
    Get-Help -Name Get-Command # Get help for a specific PowerShell function
    Get-Help -Name Get-Command -Parameter Module # Get help for a specific parameter on a specific command

    ###################################################
    # Flow Control Statements
  15. @pcgeek86 pcgeek86 revised this gist Feb 13, 2019. 1 changed file with 23 additions and 1 deletion.
    24 changes: 23 additions & 1 deletion cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    Get-Command
    Get-Help

    ###################################################
    # Flow Control Statements
    @@ -50,7 +52,27 @@ function Do-Something {
    }

    ###################################################
    # Modules
    # Working with Modules
    ###################################################

    Get-Command -Name *module* -Module mic*core # Which commands can I use to work with modules?

    Get-Module -ListAvailable # Show me all of the modules installed on my system (controlled by $env:PSModulePath)
    Get-Module # Show me all of the modules imported into the current session

    Import-Module # Explicitly import a module, from the specified filesystem path or name (must be present in $env:PSModulePath)
    Remove-Module # Remove a module from the scope of the current PowerShell session

    New-ModuleManifest # Helper function to create a new module manifest. You can create it by hand instead.

    New-Module -Name trevor -ScriptBlock { # Create an in-memory PowerShell module (advanced users)
    function Add($a,$b) { $a + $b } }

    New-Module -Name trevor -ScriptBlock { # Create an in-memory PowerShell module, and make it visible to Get-Module (advanced users)
    function Add($a,$b) { $a + $b } } | Import-Module

    ###################################################
    # Module Management
    ###################################################

    Get-Command -Module PowerShellGet # Explore commands to manage PowerShell modules
  16. @pcgeek86 pcgeek86 revised this gist Feb 13, 2019. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,19 @@

    ###################################################
    # Conditional Statements
    # 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
    ###################################################
  17. @pcgeek86 pcgeek86 revised this gist Feb 13, 2019. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,9 @@ Find-Module -Name ps* # Find modules in th

    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 PowerShell Gallery
    Uninstall-Module -Name NameIT # Uninstall module called "NameIT", only if it was installed via Install-Module

    Register-PSRepository -Name <repo> -SourceLocation <uri> # Configure a private PowerShell module registry
    Register-PSRepository -Name <repo> -SourceLocation <uri> # Configure a private PowerShell module registry
    Unregister-PSRepository -Name <repo> -SourceLocation uri # Deregister a PowerShell Repository
  18. @pcgeek86 pcgeek86 revised this gist Feb 13, 2019. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -5,13 +5,15 @@

    if (1 -eq 1) { } # Do something if 1 is equal to 1

    $a = 0


    ###################################################
    # 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
  19. @pcgeek86 pcgeek86 revised this gist Feb 13, 2019. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -12,14 +12,18 @@ $a = 0
    # Variables
    ###################################################

    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 scope
    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
  20. @pcgeek86 pcgeek86 created this gist Feb 13, 2019.
    51 changes: 51 additions & 0 deletions cheatsheet.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@

    ###################################################
    # 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