Powershell – Automatically Passing Password to Remove-Computer Powershell Script

powershell

I'm trying to make a simple powershell script that will allow me run the following without prompting for the password:

remove-computer -credential ExampleDomain\ExampleName -Force -passthrough -verbose

But the problem with this is I get prompted to input a password every time, and I need to just pass it the password without it asking every time. This is for my own use in a lab environment so I'm not worried about storing in plain text, but any more secure suggestions are welcome of course.

Also in this environment I'm logging in as one user, but authenticating on the domain with a different set of credentials, so I cant use any "read from stored credentials" type solution either.

Also I've learned that this makes a difference so I'll mention I'm locked to using PowerShell V2 in the environment.

Best Answer

The -Credential parameter is actually expecting a full PSCredential object rather than just a username (which is why it prompts for the password). So let's give it what it wants.

$securePass = ConvertTo-SecureString "MySecretPassword" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("ExampleDomain\ExampleName", $securePass)
Remove-Computer -Credential $cred -Force -Passthrough -Verbose