Windows Powershell Credential Request

Hey guys,

I just finished working on a script of mine which starts with the following line.

[pre]
$Credential = Get-Credential -UserName “DOMAIN\AdminJoe” -Message “Provide Admin credentials:”
[/pre]

Now, when the Windows Powershell Credential Request prompts, there’s a drop down list. Because of the line above, DOMAIN\AdminJoe is already filled in. I was wondering if it’s possible to add multiple accounts to this drop down list, so that a colleague of mine can execute the script with his credentials (without entering his account name over and over again).

Looking forward to the response!

Regards,
Tom

Hi Tom,

The UserName parameter is a string parameter and cannot accept multiple inputs, so it is not possible to have a drop down with multiple inputs.

One way you can do it is by accepting a -Credential parameter and use the Export-CliXml and Import-CliXml cmdlets.

[pre]
param (
[pscredential]$Credential
)
[/pre]

Then, when a colleague calls the script, they can do something like:

[pre]
Export-CliXml -Path “<Some local path>.xml” -InputObject (Get-Credential -UserName <username>
$Credential = Import-CliXml -Path "<Some local path>.xml>

.\Script.ps1 -Credential $Credential
[/pre]

You only need to run the Export-CliXml once and will store in the given location in a secured xml format. Then the Import-CliXml cmdlet will import their credentials, and will only import the credentials from the context of the user that ran the Export-CliXml cmdlet.

pwshliquori

Hey pwshliquori,

I should have formulated myself better i.e. that I had read the help file of Get-Credential (where the UserName parameter didn’t accept multiple inputs). My question was more like if there were any other ways to do this (which you gave me). Some working points for me!

The solution you gave me works like a charm.

Thanks for your help! Appreciate it.

Regards,
Tom

You can actually do this! You’ll need the powershell module CredentialManager:

Install-Module CredentialManager

Then store a username like this:

Get-Credential | New-StoredCredential -Target "user1@domain.com" -persist Enterprise

Enter ONLY the username field and hit ok.

Repeat for user2:

Get-Credential | New-StoredCredential -Target "user2@domain.com" -persist Enterprise

Again ONLY the username field and hit ok.

Now, anytime you use simply (Get-Credential) on that machine, you can choose from the user dropdown, and supply the password yourself.