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

This topic contains 3 replies, has 3 voices, and was last updated by  Kamil 4 days, 12 hours ago.

  • Author
  • #95814


    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

    Matt Bloomfield

    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


    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


    Thank you very much.

You must be logged in to reply to this topic.