Get-Process | Select-Object Name, Id
Select-Object can also be used to create custom properties. This feature is useful when there is a need to rename a property, or add related information.@{Name = 'NewProperty'; Expression = { 'Code to generate the value' }}
@{Label = 'NewProperty'; Expression = { 'Code to generate the value' }}
The label key, while permissible, is rarely used; name is the most common choice. Select-Object allows short-hand, the keys Name and Expression can be shortened to "n" and "e" respectively.@{n = 'NewProperty'; e = { 'Code to generate the value' }}
The example below selects 3 properties from Get-Process. The third value, WorkingSet, which holds the amount of memory a process uses, is converted from bytes to MB using a custom property.Get-Process | Select Name, Id, @{n='WorkingSet';e={ $_.WorkingSet / 1MB }}
Select-Object can include any number of custom properties, each separated by a comma. The example below adds a second property, the owner of the file (based on the NTFS Access Control List) if the Path property of the process is set.Get-Process | Select Name,
Id,
@{n='WorkingSet';e={ [Math]::Round($_.WorkingSet / 1MB, 2) }},
@{n='Owner';e={ if ($_.Path) { (Get-Acl $_.Path).Owner } }}
Get-ADGroup -filter * | Select-Object Name, @{n='Members';e={ Get-ADGroup -LdapFilter "(memberOf:1.2.840.113556.1.4.1941:=$($_.DistinguishedName))" | Select-Object -ExpandProperty Name }}
This uses a specific OID to expand membership.Get-ADGroup -filter * | Select-Object Name, @{n='Members';e={ Get-ADObject -LdapFilter "(memberOf:1.2.840.113556.1.4.1941:=$($_.DistinguishedName))" | Select-Object -ExpandProperty Name }}
If you actually have groups with that number of members you need to go back to the original requirement, think about why you're doing this, and evaluate if this is really the best way. It's a horribly expensive operation, recursive group expansion is a big thing to ask a DC to do, doing it fora large number of groups is not sensible at all.
function Get-ADSIGroup {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[String]$Name
)
$searcher = [ADSISearcher]"(&(objectClass=group)(objectCategory=group)(name=$Name))"
$searcher.PageSize = 1000
$searcher.PropertiesToLoad.AddRange(@('name', 'distinguishedName'))
$searcher.FindAll() | ForEach-Object {
[PSCustomObject]@{
Name = $_.Properties['name'][0]
DistinguishedName = $_.Properties['distinguishedName'][0]
}
}
}
function Get-ADSIGroupMember {
[CmdletBinding()]
param(
[Parameter(ValueFromPipelineByPropertyName = $true)]
[String]$DistinguishedName,
[Switch]$Recursive
)
process {
if ($Recursive) {
$LdapFilter = "(memberOf:1.2.840.113556.1.4.1941:=$DistinguishedName)"
} else {
$LdapFilter = "(memberOf=$DistinguishedName)"
}
$searcher = [ADSISearcher]$LdapFilter
$searcher.PageSize = 1000
$searcher.PropertiesToLoad.AddRange(@('name', 'distinguishedName'))
$searcher.FindAll() | ForEach-Object {
[PSCustomObject]@{
Name = $_.Properties['name'][0]
DistinguishedName = $_.Properties['distinguishedName'][0]
}
}
}
}
Get-ADSIGroup -Name * | Select-Object Name, @{n='Members';e={ $_ | Get-ADSIGroupMember -Recursive | Select-Object -ExpandProperty Name }}
Open in new window