System.Management.Automation.Credential() in the Param block

Welcome Forums General PowerShell Q&A System.Management.Automation.Credential() in the Param block

This topic contains 3 replies, has 3 voices, and was last updated by

11 months, 1 week ago.

  • Author
  • #95814

    Points: 21
    Rank: Member

    Good day.
    i have this code that i downloaded from GitHub.
    i don't understand the meaning of line 15.
    its not a method, its not a type. What is it?

    Function Test-Credential {
        Param (
                Mandatory = $true,
                ValueFromPipeLine = $true,
                ValueFromPipelineByPropertyName = $true
            $Domain = $Credential.GetNetworkCredential().Domain
        Begin {


  • #95847

    Points: 41
    Rank: Member

    I'd not seen this before either. Here's a good post that explains how to add credentials to PowerShell functions:

    The purpose of Line 15 is to allow the user to pass in a username as a string; they will then receive a prompt to enter a password.

  • #95852

    Points: 780
    Helping Hand
    Rank: Major Contributor

    I have seen this used, though I personally have not needed it. Yet, this has been a use for a while now for some.
    See this discussion from an archive of stuff I keep around.

    How to convert parameter type into a different object type

    I'm doing some scripting in PowerShell, and I was wondering if there's a way to "declare" a parameter "X" the same way parameter "-Credential" is declared, for example in Get-WMIObject cmdlet.

    Let me be more specific. The Credential parameter in almost all cmdlets is a PSCredential Object. But, the argument can be either a PSCredential Object or, a String Object with the username.


    When the Credential parameter is PSCredential, you cannot pass a string to it: get-something -cred adi, you'll get a ' Cannot process argument transformation' error. using the 'Credential()' transformation solves this. Now you can do 'get-something -cred adi' and the have the credential dialog invoked and populated with the username.


  • #95978

    Points: 21
    Rank: Member

    Thank you very much.

The topic ‘System.Management.Automation.Credential() in the Param block’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort