Getting started with PowerShellLoopsOperatorsUsing ShouldProcessPowerShell ClassesSwitch statementWorking with ObjectsUsing existing static classesBasic Set OperationsPowerShell FunctionsSending EmailHandling Secrets and CredentialsPowershell RemotingPowerShell "Streams"; Debug, Verbose, Warning, Error, Output and InformationVariables in PowerShellCommunicating with RESTful APIsWorking with the PowerShell pipelinePowerShell Background JobsReturn behavior in PowerShellWorking with XML FilesIntroduction to PsakeUsing the progress barStringsTCP Communication with PowerShellSharePoint ModuleAliasesAutomatic VariablesEnvironment VariablesPowershell profilesEnforcing script prerequisitesUsing the Help SystemSplattingDesired State ConfigurationSigning ScriptsSecurity and CryptographyCSV parsingIntroduction to PesterModules, Scripts and FunctionsPowerShell.exe Command-LineCommon parametersParameter setsRegular ExpressionsPowerShell Dynamic ParametersWMI and CIMGUI in PowershellConditional logicURL Encode/DecodeMongoDBRunning ExecutablesError handlingHashTablesActiveDirectory modulepowershell sql queriesAutomatic Variables - part 2Package managementCmdlet NamingBuilt-in variablesCreating DSC Class-Based ResourcesPowershell ModulesPowerShell WorkflowsHow to download latest artifact from Artifactory using Powershell script (v2.0 or below)?Calculated PropertiesSpecial OperatorsAnonymize IP (v4 and v6) in text file with PowershellComment-based helpAmazon Web Services (AWS) Simple Storage Service (S3)Amazon Web Services (AWS) RekognitionPSScriptAnalyzer - PowerShell Script AnalyzerNaming ConventionsEmbedding Managed Code (C# | VB)Archive ModuleInfrastructure AutomationScheduled tasks moduleISE module

Basic Set Operations

Other topics

Filtering: Where-Object / where / ?

Filter an enumeration by using a conditional expression

Synonyms:

Where-Object
where
?

Example:

$names = @( "Aaron", "Albert", "Alphonse","Bernie", "Charlie", "Danny", "Ernie", "Frank")

$names | Where-Object { $_ -like "A*" }
$names | where { $_ -like "A*" }
$names | ? { $_ -like "A*" }

Returns:

Aaron
Albert
Alphonse

Ordering: Sort-Object / sort

Sort an enumeration in either ascending or descending order

Synonyms:

Sort-Object
sort

Assuming:

$names = @( "Aaron", "Aaron", "Bernie", "Charlie", "Danny" )

Ascending sort is the default:

$names | Sort-Object
$names | sort

Aaron
Aaron
Bernie
Charlie
Danny

To request descending order:

$names | Sort-Object -Descending
$names | sort -Descending

Danny
Charlie
Bernie
Aaron
Aaron

You can sort using an expression.

$names | Sort-Object { $_.length }

Aaron
Aaron
Danny
Bernie
Charlie

Grouping: Group-Object / group

You can group an enumeration based on an expression.

Synonyms:

Group-Object
group

Examples:

$names = @( "Aaron", "Albert", "Alphonse","Bernie", "Charlie", "Danny", "Ernie", "Frank")

$names | Group-Object -Property Length
$names | group -Property Length

Response:

CountNameGroup
45{Aaron, Danny, Ernie, Frank}
26{Albert, Bernie}
18{Alphonse}
17{Charlie}

Projecting: Select-Object / select

Projecting an enumeration allows you to extract specific members of each object, to extract all the details, or to compute values for each object

Synonyms:

Select-Object
select

Selecting a subset of the properties:

$dir = dir "C:\MyFolder"

$dir | Select-Object Name, FullName, Attributes
$dir | select Name, FullName, Attributes
NameFullNameAttributes
ImagesC:\MyFolder\ImagesDirectory
data.txtC:\MyFolder\data.txtArchive
source.cC:\MyFolder\source.cArchive

Selecting the first element, and show all its properties:

$d | select -first 1 *
PSPath
PSParentPath
PSChildName
PSDrive
PSProvider
PSIsContainer
BaseName
Mode
Name
Parent
Exists
Root
FullName
Extension
CreationTime
CreationTimeUtc
LastAccessTime
LastAccessTimeUtc
LastWriteTime
LastWriteTimeUtc
Attributes

Syntax:

  • Group-Object

  • Group-Object -Property <propertyName>

  • Group-Object -Property <propertyName>, <propertyName2>

  • Group-Object -Property <propertyName> -CaseSensitive

  • Group-Object -Property <propertyName> -Culture <culture>

  • Group-Object -Property <ScriptBlock>

  • Sort-Object

  • Sort-Object -Property <propertyName>

  • Sort-Object -Property <ScriptBlock>

  • Sort-Object -Property <propertyName>, <propertyName2>

  • Sort-Object -Property <propertyObject> -CaseSensitive

  • Sort-Object -Property <propertyObject> -Descending

  • Sort-Object -Property <propertyObject> -Unique

  • Sort-Object -Property <propertyObject> -Culture <culture>

Contributors

Topic Id: 1557

Example Ids: 5057,5058,5059,5060

This site is not affiliated with any of the contributors.