Tag Archives: End

New-TimeSpan

NAME
    New-TimeSpan

SYNOPSIS
    Creates a TimeSpan object.

SYNTAX
    New-TimeSpan [[-Start] <DateTime>] [[-End] <DateTime>] [<CommonParameters>]

    New-TimeSpan [-Days <int>] [-Hours <int>] [-Minutes <int>] [-Seconds <int>] [<CommonParameters>]

DESCRIPTION
    The New-TimeSpan cmdlet creates a TimeSpan object that represents a time. interval You can use a TimeSpan object to add or subtract time from DateTime objects.

    Without parameters, a “New-TimeSpan” command returns a timespan object that represents a time interval of zero.

PARAMETERS
    -Days <int>
        Indicates the days in the time span. The default is 0.

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

    -End <DateTime>
        Indicates the end of a time span. The default is the current date and time.

        Required?                    false
        Position?                    2
        Default value                Current date and time
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Hours <int>
        Indicates the hours in the time span. The default is zero.

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

    -Minutes <int>
        Indicates the minutes in the time span. The default is 0.

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

    -Seconds <int>
        Indicates the length of the time span in seconds. The default is 0.

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

    -Start <DateTime>
        Indicates the start of a time span. Enter a string that represents the date and time, such as “3/15/09” or a DateTime object, such as one from a Get-Date command.

        The default is the current date and time.

        Required?                    false
        Position?                    1
        Default value                Current date and time
        Accept pipeline input?     true (ByValue, ByPropertyName)
        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.DateTime
        You can pipe a DateTime object that represents that start time to New-TimeSpan.

OUTPUTS
    System.Timespan
        New-TimeSpan returns an object that represents the time span.

NOTES

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

    C:\PS>$timespan = New-TimeSpan -hour 1 -minute 25

    Description
    ———–
    This command creates a TimeSpan object with a duration of 1 hour and 25 minutes and stores it in a Variable named $timespan. It displays a representation of the TimeSpan object.

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

    C:\PS>New-TimeSpan -End (Get-Date -year 2010 -month 1 -day 1)

    Description
    ———–
    This example creates a new TimeSpan object that represents the interval between the time that the command is run and January 1, 2010.

    This command does not require the Start parameter, because the default value of the Start parameter is the current date and time.

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

    C:\PS>$90days = New-TimeSpan -days 90

    C:\PS> (Get-Date) + $90days

    Description
    ———–
    These commands return the date that is 90 days after the current date.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113360
    Set-Date
    Get-Date

ForEach-Object

NAME
    ForEach-Object

SYNOPSIS
    Performs an operation against each of a set of input objects.

SYNTAX
    ForEach-Object [-Process] <ScriptBlock[]> [-Begin <scriptblock>] [-End <scriptblock>] [-InputObject <psobject>] [<CommonParameters>]

DESCRIPTION
    The ForEach-Object cmdlet performs an operation on each of a set of input objects. The input objects can be piped to the cmdlet or specified by using the InputObject parameter.

    The operation to perform is described within a script block that is provided to the cmdlet as the value of the Process parameter. The script block can contain any Windows PowerShell script.

    Within the script block, the current input object is represented by the $_ Variable.
    In addition to using the script block that describes the operations to be carried out on each input object, you can provide two additional script blocks. One, specified as the value of the Begin parameter, runs before the first input object is processed. The other, specified as the value of the End parameter, runs after the last input object is processed.

    The results of the evaluation of all the script blocks, including the ones specified with Begin and End, are passed down the pipeline.

PARAMETERS
    -Begin <scriptblock>
        Specifies a script block to run before processing any input objects.

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

    -End <scriptblock>
        Specifies a script block to run after processing all input objects.

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

    -InputObject <psobject>
        Accepts an object that the script block specified in the process parameter will act upon. Enter a Variable that contains the objects, or type a command or expression that gets the objects.

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

    -Process <ScriptBlock[]>
        Specifies the script block that is applied to each incoming object.

        Required?                    true
        Position?                    1
        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 ForEach-Object.

OUTPUTS
    System.Management.Automation.PSObject
        The objects that ForEach-Object returns are determined by the input.

NOTES

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

    C:\PS>30000,56798,12432 | ForEach-Object -Process {$_/1024}

    Description
    ———–
    This command accepts an array of integers, divides each one of them by 1024, and displays the results.

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

    C:\PS>Get-ChildItem C:\ | ForEach-Object -Process { $_.length / 1024 }

    Description
    ———–
    This command retrieves the files and directories in the root of the C: drive, and it returns and displays the size of each of them. The zeros represent directories in which no file size was available.

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

    C:\PS>$events = Get-Eventlog -logname system -newest 1000

    C:\PS> $events | ForEach-Object -Begin {Get-Date} -Process {Out-File -filepath events.txt -append -InputObject $_.message} -End {Get-Date}

    Description
    ———–
    This command retrieves the 1000 most recent events from the system log and stores them in the $events Variable. It then pipes the events to the ForEach-Object cmdlet. The Begin parameter displays the current date and time. Next, the Process parameter uses the Out-File cmdlet to create a text file named events.txt and stores the message property of each of the events in that file. Last, the End parameter is used to display the date and time after all of the processing has completed.

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

    C:\PS>Get-Itemproperty -path hkcu:\Network\* | ForEach-Object {Set-Itemproperty -path $_.pspath -name RemotePath -value $_.RemotePath.ToUpper();}

    Description
    ———–
    This command changes the value of the RemotePath Registry entry in all of the subkeys under the HKCU:\Network key to uppercase text. You can use this format to change the form or content of a Registry entry value.

    Each subkey in the Network key represents a mapped network drive that will reconnect at logon. The RemotePath entry contains the UNC path of the connected drive. For example, if you map the E: drive to \\Server\Share, there will be an E subkey of HKCU:\Network and the value of the RemotePath Registry entry in the E subkey will be \\Server\Share.

    The command uses the Get-ItemProperty cmdlet to get all of the subkeys of the Network key and the Set-ItemProperty cmdlet to change the value of the RemotePath Registry entry in each key. In the Set-ItemProperty command, the path is the value of the PSPath property of the Registry key. (This is a property of the Microsoft .NET Framework object that represents the Registry key; it is not a Registry entry.) The command uses the ToUpper() method of the RemotePath value, which is a string (REG_SZ).

    Because Set-ItemProperty is changing the property of each key, the ForEach-Object cmdlet is required to access the property.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113300