Category Archives: AuthenticodeSignature

Set-AuthenticodeSignature

NAME
    Set-AuthenticodeSignature

SYNOPSIS
    Adds an Authenticode signature to a Windows PowerShell script or other file.

SYNTAX
    Set-AuthenticodeSignature [-FilePath] <string[]> [-Certificate] <X509Certificate2> [-Force] [-HashAlgorithm <string>] [-IncludeChain <string>] [-TimestampServer <string>] [-Confirm] [-WhatIf] [<CommonParameters>]

DESCRIPTION
    The Set-AuthenticodeSignature cmdlet adds an Authenticode signature to any file that supports Subject Interface Package (SIP).

    In a Windows PowerShell script file, the signature takes the form of a block of text that indicates the end of the instructions that are executed in the script. If there is a signature in the file when this cmdlet runs, that signature is removed.

PARAMETERS
    -Certificate <X509Certificate2>
        Specifies the Certificate that will be used to sign the script or file. Enter a Variable that stores an object representing the Certificate or an expression that gets the Certificate.

        To find a Certificate, use Get-PfxCertificate or use the Get-ChildItem cmdlet in the Certificate (Cert:) drive. If the Certificate is not valid or does not have code-signing authority, the command fails.

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

    -FilePath <string[]>
        Specifies the path to a file that is being signed.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue, ByPropertyName)
        Accept wildcard characters? false

    -Force [<SwitchParameter>]
        Allows the cmdlet to append a signature to a read-only file. Even using the Force parameter, the cmdlet cannot override security restrictions.

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

    -HashAlgorithm <string>
        Specifies the hashing algorithm that Windows uses to compute the digital signature for the file. The default is SHA1, which is the Windows default hashing algorithm.

        Files that are signed with a different hashing algorithm might not be recognized on other systems.

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

    -IncludeChain <string>
        Determines which Certificates in the Certificate trust chain are included in the digital signature. “NotRoot” is the default.

        Valid values are:

        — Signer: Includes only the signer’s Certificate.

        — NotRoot: Includes all of the Certificates in the Certificate chain, except for the root authority.

        –All: Includes all the Certificates in the Certificate chain.

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

    -TimestampServer <string>
        Uses the specified time stamp server to add a time stamp to the signature. Type the URL of the time stamp server as a string.

        The time stamp represents the exact time that the Certificate was added to the file. A time stamp prevents the script from failing if the Certificate expires because users and programs can verify that the Certificate was valid at the time of signing.

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

    -Confirm [<SwitchParameter>]
        Prompts you for confirmation before executing the command.

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

    -WhatIf [<SwitchParameter>]
        Describes what would happen if you executed the command without actually executing the command.

        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.String
        You can pipe a string that contains the file path to Set-AuthenticodeSignature.

OUTPUTS
    System.Management.Automation.Signature

NOTES

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

    C:\PS>$cert=Get-ChildItem -Path cert:\CurrentUser\my -CodeSigningCert

    C:\PS>Set-AuthenticodeSignature -FilePath PsTestInternet2.ps1 -Certificate $cert

    Description
    ———–
    These commands retrieve a code-signing Certificate from the Windows PowerShell Certificate provider and use it to sign a Windows PowerShell script.

    The first command uses the Get-ChildItem cmdlet and the Windows PowerShell Certificate provider to get the Certificates in the Cert:\CurrentUser\My subdirectory of the Certificate store. (The Cert: drive is the drive exposed by the Certificate provider.) The CodeSigningCert parameter, which is supported only by the Certificate provider, limits the Certificates retrieved to those with code-signing authority. The command stores the result in the $cert Variable.

    The second command uses the Set-AuthenticodeSignature cmdlet to sign the PSTestInternet2.ps1 script. It uses the FilePath parameter to specify the name of the script and the Certificate parameter to specify that the Certificate is stored in the $cert Variable.

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

    C:\PS>$cert = Get-PfxCertificate C:\Test\Mysign.pfx

    C:\PS>Set-AuthenticodeSignature -FilePath ServerProps.ps1 -Cert $cert

    Description
    ———–
    These commands use the Get-PfxCertificate cmdlet to find a code signing Certificate. Then, they use it to sign a Windows PowerShell script.

    The first command uses the Get-PfxCertificate cmdlet to find the C:\Test\MySign.pfx Certificate and store it in the $cert Variable.

    The second command uses Set-AuthenticodeSignature to sign the script. The FilePath parameter of Set-AuthenticodeSignature specifies the path to the script file being signed and the Cert parameter passes the $cert Variable containing the Certificate to Set-AuthenticodeSignature.

    If the Certificate file is password protected, Windows PowerShell prompts you for the password.

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

    C:\PS>Set-AuthenticodeSignature -FilePath c:\scripts\Remodel.ps1 -Certificate $cert -IncludeChain All -TimestampServer “http://timestamp.fabrikam.com/scripts/timstamper.dll”

    Description
    ———–
    This command adds a digital signature that includes the root authority in the trust chain, and it is signed by a third-party timestamp server.

    The command uses the FilePath parameter to specify the script being signed and the Certificate parameter to specify the Certificate that is saved in the $cert Variable. It uses the IncludeChain parameter to include all of the signatures in the trust chain (including the root authority). It also uses the TimeStampServer parameter to add a timestamp to the signature. This prevents the script from failing when the Certificate expires.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113391
    about_Signing
    about_execution_policies
    Get-AuthenticodeSignature
    Get-PfxCertificate
    Get-ExecutionPolicy
    Set-ExecutionPolicy

Get-AuthenticodeSignature

NAME
    Get-AuthenticodeSignature

SYNOPSIS
    Gets information about the Authenticode signature in a file.

SYNTAX
    Get-AuthenticodeSignature [-FilePath] <string[]> [<CommonParameters>]

DESCRIPTION
    The Get-AuthenticodeSignature cmdlet gets information about the Authenticode signature in a file. If the file is not signed, the information is retrieved, but the fields are blank.

PARAMETERS
    -FilePath <string[]>
        Specifies the path to the file being examined. Wildcards are permitted, but they must lead to a single file. The parameter name (“FilePath”) is optional.

        Required?                    true
        Position?                    1
        Default value
        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.String
        You can pipe a string that contains a file path to Get-AuthenticodeSignature.

OUTPUTS
    System.Management.Automation.Signature
        Get-AuthenticodeSignature returns a signature object for each signature that it gets.

NOTES

        For information about Authenticode signatures in Windows PowerShell, see about_Signing.

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

    C:\PS>Get-AuthenticodeSignature -FilePath C:\Test\NewScript.ps1

    Description
    ———–
    This command gets information about the Authenticode signature in the NewScript.ps1 file. It uses the FilePath parameter to specify the file.

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

    C:\PS>Get-AuthenticodeSignature test.ps1, test1.ps1, sign-file.ps1, makexml.ps1

    Description
    ———–
    This command gets information about the Authenticode signature in the four files listed at the command line. In this command, the name of the FilePath parameter, which is optional, is omitted.

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

    C:\PS>Get-ChildItem $pshome\*.* | ForEach-Object {Get-AuthenticodeSignature $_} | where {$_.status -eq “Valid”}

    Description
    ———–
    This command lists all of the files in the $pshome directory that have a valid Authenticode signature. The $pshome automatic Variable contains the path to the Windows PowerShell installation directory.

    The command uses the Get-ChildItem cmdlet to get the files in the $pshome directory. It uses a pattern of *.* to exclude directories (although it also excludes files without a dot in the filename).

    The command uses a pipeline operator (|) to send the files in $pshome to the ForEach-Object cmdlet, where Get-AuthenticodeSignature is called for each file.

    The results of the Get-AuthenticodeSignature command are sent to a Where-Object command that selects only the signature objects with a status of “Valid”.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113307
    about_Signing
    about_execution_policies
    Set-AuthenticodeSignature
    Get-ExecutionPolicy
    Set-ExecutionPolicy