Tag Archives: Unique

Sort-Object

NAME
    Sort-Object

SYNOPSIS
    Sorts objects by property values.

SYNTAX
    Sort-Object [[-Property] <Object[]>] [-CaseSensitive] [-Culture <string>] [-Descending] [-InputObject <psobject>] [-Unique] [<CommonParameters>]

DESCRIPTION
    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.

PARAMETERS
    -CaseSensitive [<SwitchParameter>]
        Indicates that the sort should be case sensitive. By default, sorting is not case sensitive.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Culture <string>
        Specifies the cultural configuration to use when sorting.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Descending [<SwitchParameter>]
        Sorts the objects in descending order. The default is ascending order.

        The Descending parameter applies to all properties. To sort by some properties in
        ascending order and others in descending order, you must specify their property values by using a hash table. For details, see the examples.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -InputObject <psobject>
        Specifies the objects to be sorted.

        When you use the InputObject parameter to submit a collection of items, Sort-Object receives one object that represents the collection. Because one object cannot be sorted, Sort-Object returns the entire collection unchanged.

        To sort objects, pipe them to Sort-Object.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    -Property <Object[]>
        Specifies the properties to use when sorting. Objects are sorted based on the values of these properties. Enter the names of the properties. Wildcards are permitted.

        If you specify multiple properties, the objects are first sorted by the first property. If more than one object has the same value for the first property, those objects are sorted by the second property. This process continues until there are no more specified properties or no groups of objects.

        If you do not specify properties, the cmdlet sorts based on default properties for the object type.

        The value of the Property parameter can be a new calculated property. To create a calculated, property, use a hash table. Valid keys are:

        — Expression <string> or <script block>
        — Ascending <Boolean>
        — Descending <Boolean>

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -Unique [<SwitchParameter>]
        Eliminates duplicates and returns only the unique members of the collection. You can use this parameter instead of using the Get-Unique cmdlet.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.Management.Automation.PSObject
        You can pipe the objects to be sorted to Sort-Object.

OUTPUTS
    System.Management.Automation.PSObject
        Sort-Object returns the sorted objects.

NOTES

        Sort-Object sorts objects based on the properties that you specify or the default sort properties for objects of that type.

        If an object does not have one of the specified properties, the property value for that object is interpreted by the cmdlet as NULL and is placed at the end of the sort order.

        When sorting objects, Sort-Object uses the Compare method for each property. If a property does not implement IComparable, the cmdlet converts the property value to a string and uses the Compare method for System.String.

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

        If you sort on a property whose value is an enumeration, Sort-Object sorts the enumeration values in numeric order; it does not sort the enumeration member names. For example, if you sort services by status, services with a status of “Stopped” appear before services with a status of “Running”, because the value of Status is a ServiceControllerStatus enumeration, in which “Stopped” has a value of 1 and “Running” has a value of 4.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-ChildItem | Sort-Object

    Mode                LastWriteTime     Length Name
    —-                ————-     —— —-
    -a—         9/13/2005 4:24 PM         0 0
    -a—         9/6/2005 4:19 PM         12 a.csv
    -a—         9/21/2005 3:49 PM        529 a.Ps
    -a—         8/22/2005 4:14 PM         22 a.pl
    -a—         9/27/2005 10:33 AM         24 a.txt
    -a—         9/15/2005 10:31 AM        398 a.vbs
    -a—         7/21/2005 12:39 PM     37066 a.xml
    -a—         8/28/2005 11:30 PM     5412 a.xslt
    -a—        10/25/2005 1:59 PM        125 AdamTravel.txt
    -a—         7/21/2005 9:49 AM         59 add2Num.Ps
    -a—         8/29/2005 5:42 PM     7111 Add-Content.xml
    -a—         9/21/2005 12:46 PM     8771 aliens.Ps
    -a—         8/10/2005 2:10 PM        798 array.xml
    -a—         8/4/2004 5:00 AM        110 AUTORUN.INF
    -a—         9/6/2005 4:20 PM        245 b.csv
    …

    Description
    ———–
    This command sorts the subdirectories and files in the current directory. Because no properties are specified, the files and directories are sorted in ascending alphabetical order by their default sort property, Name.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Get-ChildItem | Sort-Object -Property length

    Mode                LastWriteTime     Length Name
    —-                ————-     —— —-
    -a—         12/3/2006 5:35 PM         2 pref.txt
    -a—         9/6/2006 3:33 PM         15 count.txt
    -a—         7/26/2006 10:01 AM         30 filenoext
    -a—         8/18/2006 9:02 AM         52 temp.ps1
    -a—         8/18/2006 9:02 AM         52 temp.msh
    -a—         9/6/2006 3:33 PM         56 fivewords.txt
    -a—         7/26/2006 9:28 AM         80 date.csv
    -a—         7/29/2006 7:15 PM         84 test2.txt
    -a—         7/29/2006 7:15 PM         84 test.ps1

    Description
    ———–
    This command displays the files in the current directory in ascending order by file length.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Get-Process | Sort-Object -Property WS | Select-Object -last 5

    Handles NPM(K)    PM(K)     WS(K) VM(M) CPU(s)     Id ProcessName
    ——- ——    —–     —– —– ——     — ———–
     1105     25    44236     18932 197    93.81 2032 iexplore
     2526     66    37668     36836 221 393.27    868 svchost
        974     19    22844     45928 371    88.39 3952 WINWORD
     1371     22    42192     61872 323    75.75 1584 INFOPATH
     2145     58    93088     70680 619 396.69 3908 OUTLOOK

    Description
    ———–
    This command displays the five processes on the computer with the greatest memory use based on the size of their working sets.

    The command uses the Get-Process cmdlet to get a list of processes. It uses a pipeline operator (|) to send the results to the Sort-Object cmdlet, which sorts the objects in working-set order.

    Another pipeline operator sends the results to the Select-Object, which displays only the last five items in the list.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Get-History | Sort-Object -Descending

    Id CommandLine
    — ———–
    51 Get-History | sort -Descending
    50 Get-History | sort -Descending
    49 Get-History | sort -Descending
    48 Get-History | sort -Descending
    47 Get-History | sort -Descending
    46 Get-History | sort -Descending
    45 Get-History | sort -Descending
    44 cd $pshome
    43 Get-ChildItem | Sort-Object
    42 gci *.txt

    Description
    ———–
    This command sorts HistoryInfo objects using the Id property as the default key.

    ————————– EXAMPLE 5 ————————–

    C:\PS>C:\PS> Get-Service | Sort-Object -Property `
    @{Expression=”Status”;Descending=$true}, `
    @{Expression=”DisplayName”;Descending=$false}

    Status Name             DisplayName
    —— —-             ———–
    Running ALG                Application Layer Gateway Service
    Running Ati HotKey Poller Ati HotKey Poller
    Running wuauserv         Automatic Updates
    Running BITS             Background Intelligent Transfer Ser…
    Running Client for NFS     Client for NFS
    …
    Stopped clr_optimizatio… .NET Runtime Optimization Service v…
    Stopped Alerter            Alerter
    Stopped AppMgmt            Application Management
    Stopped aspnet_state     ASP.NET State Service
    Stopped ATI Smart         ATI Smart
    Stopped ClipSrv            ClipBook

    Description
    ———–
    This command displays the services on the computer in descending Status order and ascending DisplayName order.

    The command uses the Get-Service cmdlet to get the services on the computer. It uses a pipeline operator (|) to send services to the Sort-Object cmdlet.

    To sort one property in ascending order and another property in descending order, the command uses a hash table for the value of the Property parameter. The hash table uses an Expression key to specify the property name and an Ascending or Descending key to specify the sort order.

    The resulting display, which sorts the Status values in descending order, lists properties with a Status value of “Running” before those with a Status value of “Stopped”. When sorted in ascending order, “Stopped” appears before “Running”, because Status is an enumerated property in which the value of “Stopped” (1) is less than the value of “Running” (4).

    ————————– EXAMPLE 6 ————————–

    C:\PS>Get-ChildItem *.txt | Sort-Object -Property @{Expression={$_.LastWriteTime – $_.CreationTime}; Ascending=$false} | Format-Table LastWriteTime, CreationTime

    LastWriteTime                         CreationTime
    ————-                         ————
    2/21/2006 10:22:20 AM                 10/3/2005 4:19:40 PM
    2/27/2006 8:14:24 AM                    2/23/2006 10:41:08 PM
    2/24/2006 1:26:19 PM                    2/23/2006 11:23:36 PM
    1/5/2006 12:01:35 PM                    1/5/2006 11:35:30 AM
    2/24/2006 9:25:40 AM                    2/24/2006 9:22:24 AM
    2/24/2006 9:40:01 AM                    2/24/2006 9:39:41 AM
    2/21/2006 10:21:30 AM                 2/21/2006 10:21:30 AM

    Description
    ———–
    This command sorts text files in descending order by the time span between CreationTime and LastWriteTime.

    ————————– EXAMPLE 7 ————————–

    C:\PS>Get-Content servers.txt

    localhost
    test01
    server01
    server02
    localhost
    server01

    C:\PS> Get-Content servers.txt | Sort-Object -Unique
    localhost
    server01
    server02
    test01

    Description
    ———–
    These commands sort the names of servers in a text file. The second command uses the Sort-Object cmdlet with the Unique parameter to return a sorted list without duplicates.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113403
    Group-Object

Select-Object

NAME
    Select-Object

SYNOPSIS
    Selects specified properties of an object or set of objects. It can also select unique objects from an array of objects, or it can select a specified number of objects from the beginning or end of an array of objects.

SYNTAX
    Select-Object [[-Property] <Object[]>] [-ExcludeProperty <string[]>] [-ExpandProperty <string>] [-First <int>] [-InputObject <psobject>] [-Last <int>] [-Skip <int>] [-Unique] [<CommonParameters>]

    Select-Object [-Index <Int32[]>] [-InputObject <psobject>] [-Unique] [<CommonParameters>]

DESCRIPTION
    The Select-Object cmdlet gets only the specified properties of an object or set of objects. It can also select unique objects from an array of objects, or it can select a specified number of objects from the beginning or end of an array of objects.

    If you use Select-Object to select specified properties, it copies the values of those properties from the input objects and creates new objects that have the specified properties and copied values.

    Use the Property parameter to specify the properties you want to select. Or, use the First, Last, Unique, Skip, and Index parameters to select particular objects from an array of input objects. For more specific object filtering, use the Where-Object cmdlet.

PARAMETERS
    -ExcludeProperty <string[]>
        Removes the specifies properties from the selection. Wildcards are permitted. This parameter is effective only when the command also includes the Property parameter.

        The value of the property parameter can be a calculated property, which is a hash table that specifies a name and calculates a value for the property display. Valid keys are:

         — Name or Label <string>
         — Expression <string> or <scriptblock>

        For more information, see the examples.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -ExpandProperty <string>
        Specifies a property to select, and indicates that an attempt should be made to expand that property. Wildcards are permitted in the property name.

        For example, if the specified property is an array, each value of the array is included in the output. If the property contains an object, the properties of that object are displayed in the output.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -First <int>
        Specifies the number of objects to select from the beginning of an array of input objects.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Index <Int32[]>
        Selects objects from an array based on their index values. Enter the indexes in a comma-separated list.

        Indexes in an array begin with 0, where 0 represents the first value and (n-1) represents the last value.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -InputObject <psobject>
        Specifies objects to send to the cmdlet through the pipeline. This parameter enables you to pipe objects to Select-Object.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    -Last <int>
        Specifies the number of objects to select from the end of an array of input objects.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Property <Object[]>
        Specifies the properties to select. Wildcards are permitted.

        The value of the Property parameter can be a new calculated property. To create a calculated, property, use a hash table. Valid keys are:
        — Name (or Label) <string>
        — Expression <string> or <script block>

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -Skip <int>
        Skips (does not select) the specified number of items. By default, the Skip parameter counts from the beginning of the array or list of objects, but if the command uses the Last parameter, it counts from the end of the list or array.

        Unlike the Index parameter, which starts counting from 0, the Skip parameter begins at 1.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Unique [<SwitchParameter>]
        Specifies that if a subset of the input objects has identical properties and values, only a single member of the subset will be selected.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.Management.Automation.PSObject
        You can pipe any object to Select-Object.

OUTPUTS
    System.Management.Automation.PSObject

NOTES

        You can also refer to Select-Object by its built-in Alias, “select”. For more information, see about_aliases.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-Process | Select-Object ProcessName,Id,WS

    Description
    ———–
    This command displays a list of processes. Only the name, ID, and working set (WS) properties of the processes are displayed.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Get-Process | Select-Object processname -ExpandProperty modules |
    Format-List

    ProcessName     : 00THotkey
    Size             : 256
    Company         : TOSHIBA Corporation
    FileVersion     : 1, 0, 0, 27
    ProductVersion    : 6, 2, 0, 0
    Description     : THotkey
    Product         : TOSHIBA THotkey
    ModuleName        : 00THotkey.exe
    FileName         : C:\WINDOWS\system32\00THotkey.exe
    BaseAddress     : 4194304

    Description
    ———–
    This command displays information about the modules used by the processes running on a computer. It uses the ExpandProperty parameter to display the details contained within the modules property.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Get-Process | Sort-Object -Property WS | Select-Object -Last 5

    Handles NPM(K)    PM(K)     WS(K) VS(M) CPU(s)     Id ProcessName
    ——- ——    —–     —– —– ——     — ———–
     2866     320    33432     45764 203 222.41 1292 svchost
        577     17    23676     50516 265    50.58 4388 WINWORD
        826     11    75448     76712 188    19.77 3780 Ps
     1367     14    73152     88736 216    61.69    676 Ps
     1612     44    66080     92780 380 900.59 6132 INFOPATH

    Description
    ———–
    This command displays the five processes that are using the most memory. The Sort-Object cmdlet is used to sort the processes according to memory (working set) usage, and the Select-Object cmdlet is used to select only the last five members of the resulting array of objects.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Get-Process | Select-Object -Property ProcessName,@{Name=”Start Day”; Expression = {$_.StartTime.DayOfWeek}}

    ProcessName StartDay
    —-         ——–
    alg         Wednesday
    ati2evxx     Wednesday
    ati2evxx     Thursday
    …

    Description
    ———–
    This command displays the name and start day of the processes running on a computer.

    The values of the Property parameter are ProcessName and a calculated property named “Start Day.” The “Start Day” property is added by using a hash table with Name and Expression keys.

    ————————– EXAMPLE 5 ————————–

    C:\PS>”a”,”b”,”c”,”a”,”a”,”a” | Select-Object -Unique

    a
    b
    c

    Description
    ———–
    This command displays unique characters from an array of characters.

    ————————– EXAMPLE 6 ————————–

    C:\PS>$a = Get-Eventlog -log “Windows PowerShell”

    C:\PS> $a | Select-Object -index 0, ($a.count – 1)

    Description
    ———–
    These commands get the first (newest) and last (oldest) events in the Windows Powershell event log.

    The first command uses the Get-EventLog cmdlet to get all events in the Windows Powershell log. It saves the events in the $a Variable.

    The second command uses a pipeline operator (|) to send the events in $a to the Select-Object cmdlet. The Select-Object command uses the Index parameter to select items by their index number. The index for the first event is 0. The index for the last event is the number of items in $a minus 1.

    ————————– EXAMPLE 7 ————————–

    C:\PS>New-PSSession -computername (Get-Content servers.txt | Select-Object -Skip 1)

    Description
    ———–
    This command creates a new PSSession on each of the computers listed in the Servers.txt files, except for the first one.

    This command uses the Select-Object cmdlet to select all but the first computer in a list of computer names. The resulting list of computers is set as the value of the ComputerName parameter of the New-PSSession cmdlet.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113387
    Where-Object
    Group-Object
    Sort-Object