PowerShell General Concepts - Gary-Moore/developmentwiki GitHub Wiki

Verbs

Windows PowerShell recommends that cmdlet and function names have the Verb-Noun format and include an approved verb. This practice makes command names more consistent and predictable, and easier to use, especially for users who do not speak English as a first language.

Commands that use unapproved verbs run in Windows PowerShell. However, when you import a module that includes a command with an unapproved verb in its name, the Import-Module command displays a warning message.

Get All Verbs

Get-Verb

Get Verb by Name

Get-Verb -verb *s

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/functions/get-verb?view=powershell-5.1

https://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx

Commands

The Get-Command cmdlet gets all commands that are installed on the computer, including cmdlets, aliases, functions, filters, scripts, and applications. Get-Command gets the commands from Windows PowerShell modules and snap-ins and commands that were imported from other sessions. To get only commands that have been imported into the current session, use the ListImported parameter.

Get-Command <name
Get-Command -verb <verb>
Get-Command <name>

Get Commands in current session

Get-Command -ListImported

Get Commands in a Module

Get-Command -Module Microsoft.PowerShell.Security, PSScheduledJob

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-command?view=powershell-6

Show-Command

The Show-Command cmdlet lets you create a Windows PowerShell command in a command window. You can use the features of the command window to run the command or have it return the command to you.

Show-Command is a very useful teaching and learning tool. Show-Command works on all command types, including cmdlets, functions, workflows and CIM commands.

Without parameters, Show-Command displays a command window that lists all available commands in all installed modules. To find the commands in a module, select the module from the Modules drop-down list. To select a command, click the command name.

Show-Command -Name "Invoke-Command"

Objects

Select-Object

The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array.

To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters. To select object properties, use the Property parameter. When you select properties, Select-Object returns new objects that have only the specified properties.

Beginning in Windows PowerShell 3.0, Select-Object includes an optimization feature that prevents commands from creating and processing objects that are not used. When you include a Select-Object command with the First or Index parameter in a command pipeline, Windows PowerShell stops the command that generates the objects as soon as the selected number of objects is generated, even when the command that generates the objects appears before the Select-Object command in the pipeline. To turn off this optimizing behavior, use the Wait parameter.

Get-Process | Select-Object -Property ProcessName, Id, WS

Select properties using a wildcard

Get-Vegetable | select c*

Select first object and all of its properties

 Get-Vegetable | select -first 1 -Property *

Select objects with unique property

Get-Vegetable | select name -Unique

Select and expand a property

Get-Vegetable | select -ExpandProperty name

Sort-Object

The Sort-Object cmdlet sorts objects in ascending or descending order based on the values of properties of the object.

You can specify a single property or multiple properties, for a multi-key sort, and you can select a case-sensitive or case-insensitive sort. You can also direct Sort-Object to display only the objects with a unique value for a particular property.

Get-Vegetable | sort count -Descending | select count,Name

Sort unique values

Get-Vegetable | sort {$_.color.tostring()} -unique

Get-Member

To specify the object, use the InputObject parameter or pipe an object to Get-Member. To get information about static members, the members of the class, not of the instance, use the Static parameter. To get only certain types of members, such as NoteProperties, use the MemberType parameter.

Get-Service | Get-Member

https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Utility/Get-Member?view=powershell-5.1

Where-object

The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it. For example, you can use the Where-Object cmdlet to select files that were created after a certain date, events with a particular ID, or computers that use a particular version of Windows.

Get-Service | Where-Object {$_.Status -eq "Stopped"}
Get-Service | where Status -eq "Stopped"

https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Core/Where-Object?view=powershell-5.1

Formatting

Format-list

The Format-List cmdlet formats the output of a command as a list of properties in which each property is displayed on a separate line. You can use Format-List to format and display all or selected properties of an object as a list (format-list *).

Get-Service | Format-List

Format by properties

Get-Process | Format-List -Property name, basepriority, priorityclass

https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Utility/Format-List?view=powershell-5.1

Out-GridView

Get-Process | Out-GridView

https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Utility/Out-GridView?view=powershell-5.1

Help

Get-Help cmdlet (Alias - help)

Use the cmdlet to get help on a particular command:

Get-Help <command>

Get help for a topic via search term:

Get-Help *<term>*

Display one page at a time

Get-Help <command> | more

Return examples

Get-Help <command> -examples

Online help instead of via console

Get-Help <command> -online

Full description including examples

Get-Help <command> -full

About Files

Get-Help *about*
help about_aliases

Updating Help

Run as administrator

Update-Help

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-help?view=powershell-6

History

Get History of current PowerShell Session

Get-History

Run commands from the session history

Invoke-History -Id <id>

https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Core/Get-History?view=powershell-5.1

Transcript

Creates a record of all or part of a Windows PowerShell session to a text file.

Start-Transcript -Path "C:\transcripts\transcript0.txt" -NoClobber
Stop-Transcript

https://docs.microsoft.com/en-gb/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-5.1

Modules

install-module

The Install-Module cmdlet gets one or more modules that meet specified criteria from an online gallery, verifies that search results are valid modules, and copies module folders to the installation location.

When no scope is defined, or when the value of the Scope parameter is AllUsers, the module is installed to %systemdrive%:\Program Files\WindowsPowerShell\Modules. When the value of Scope is CurrentUser, the module is installed to $home\Documents\WindowsPowerShell\Modules.

You can filter your results based on minimum and exact versions of specified modules.

Install-Module -Name "MyDscModule"

Find a module and install it

Find-Module -Name "MyDSC*" | Install-Module

https://docs.microsoft.com/en-gb/powershell/module/PowershellGet/Install-Module?view=powershell-6

Update-Module

The Update-Module cmdlet installs a newer version of a PowerShell module that was installed from the online gallery by running Install-Module on the local computer.

By default, the newest version of the specified module available in online gallery is installed, unless you specify a required version. You can update an existing, installed module by specifying the name of the module; Update-Module searches $env:PSModulePath for the module that you want to update.

Running Update-Module without the Name parameter updates all modules that can be updated on the local computer.

Update all modules

Update-Module

Update modules by name

Update-Module -Name "MyDscModule"

https://docs.microsoft.com/en-gb/powershell/module/powershellget/update-module?view=powershell-6

Find-Module

The Find-Module cmdlet finds modules from the online gallery that match the specified criteria. Find-Module returns a PSGetItemInfo object for each module it finds, which you can be pipe to Install-Module to install.

If the RequiredVersion parameter is not specified, Find-Module returns the latest version of the module that is equal to or greater than the minimum version specified, or the newest version of the module if no minimum version is specified.

If the RequiredVersion parameter is specified, Find-Module only returns the version of the module that exactly matches the specified version. Find-Module searches through all available modules, because name conflicts between sources can occur.

Find-Module -Name "ContosoServer"

https://docs.microsoft.com/en-gb/powershell/module/powershellget/find-module?view=powershell-6

Save-Module

The Save-Module cmdlet saves a module locally from the specified repository for inspection. The module is not installed.

Find-Module -Name "AzureAutomationDebug" -Repository "PSGallery" | Save-Module -Path "C:\MyLocalModules\"

https://docs.microsoft.com/en-gb/powershell/module/powershellget/save-module?view=powershell-6

⚠️ **GitHub.com Fallback** ⚠️