

The first command gets a credential with the user name User01 and stores it in the $c variable. This example creates a credential that includes a user name without a domain name. Example 2 $c = Get-Credential -credential User01 However, some providers that are installed with PowerShell do not support You can use the object as input to cmdlets that request user authentication, such as those with aĬredential parameter. Of the user and saves it in the $c variable. The requested information, the cmdlet creates a PSCredential object representing the credentials When you enter the command, you are prompted for a user name and password. This command gets a credential object and saves it in the $c variable. The Message parameter to specify a customized message in the command line prompt. The Get-Credential cmdlet prompts the user for a password or a user name and password. YouĬan use the credential object in security operations. The Get-Credential cmdlet creates a credential object for a specified user name and password. "Failed to map $Share to drive $: $($error.Gets a credential object based on a user name and password.

# Check if it failed, exit if it did, and print the error. $Drive.MapNetworkDrive($DriveLetter, $Share, 'false', $Username, $PlainTextPassword) | Out-Null $ErrorActionPreference = 'Silentl圜ontinue' $Drive = New-Object -ComObject WScript.Network # Try to map the drive and hide errors (will be displayed later). $PlainTextPassword = ConvertFrom-SecureToPlain $Password $Password = Read-Host -AsSecureString 'Please enter your password (it will not be displayed)' $Username = Read-Host 'Please enter a username and domain in the form DOMAIN\USERNAME' $PlainTextPassword = ::PtrToStringAuto($PasswordPointer) # Get the plain text version of the password. $PasswordPointer = ::SecureStringToBSTR($SecurePassword) It doesn't do anything useful now if a successful drive mapping is performed it's just an example. The script will exit if a connection can not be made. PS C:\> ::PtrToStringAuto(::SecureStringToBSTR($SecureString))Įxample Script for Mapping a Network Drive With Alternate CredentialsThis demonstrates how you could check if a server is available for mapping network drives against. PS C:\> $SecureString = Read-Host -AsSecureString 'Enter password' '''NB!''' ''Be aware that you should not normally use this one-liner, as it leaves an unfreed pointer instead see the example function above.'' This guy uses some methods that seem to be Unicode-specific from the same class in a related article. Another helpful person later made me aware that this left an unfreed pointer, so make sure you check out the example function for properly handling the pointer.ĭemonstration of Converting SecureString to Plain TextĪs you can see below, the class is where the most relevant stuff seems to be. He said he thought he found it buried deep in an Exchange cmdlet somewhere. A helpful person I came across had done it and had the magic incantation required, which I am now making easily available for others who might not run into this same helpful person. I searched the web for a while, but didn't find a way to convert the secure string back into plain text. The cmdlet ''New-PSDrive'' doesn't accept the -Credential parameter in PowerShell v2. Specifically, I wanted to specify alternate credentials for mapping a network drive. The problem I ran into was when I wanted to use it in conjunction with a COM method call that needed the password in plain text. To prompt a user for a password, you can use the cmdlet ''Read-Host'' with the parameter -AsSecureString. Example Script for Mapping a Network Drive With Alternate Credentials.Demonstration of Converting SecureString to Plain Text.Getting the password out of a Get-Credential object.A Generic Function for Converting Security.SecureString Objects to Plain Text Strings.Powershell prompt for password convert securestring to plain text - Svendsen Tech Jump to page sections
