
close
close
If you run your IT operations the smart and safe way, you use a normal user credential for most of the day. When you need to do something in PowerShell that requires administrator credentials, then you can often specify an alternate credential.
get-wmiobject win32_service -filter "name = 'dns'" -computer chi-dc04 -Credential globomantics\administrator
I’m prompted for a password, and PowerShell uses the credential to connect, and I get a result. This gets tedious if I have to do that every time I want to use an alternate credential. One solution is to save the credential to a variable with Get-Credential.
advertisment
$da = Get-Credential globomantics\administrator
Now I can use that instead.
get-wmiobject win32_service -filter "name = 'dns'" -computer chi-dc04 –Credential $da
I can use this variable with any command that can use a PSCredential.
Testing an alternate credential (Image Credit: Jeff Hicks)
$PSDefaultParameterValues.Add("*:Credential",$da)
Now I don’t have to think about adding the –Credential parameter; PowerShell does it for me automatically.
Testing the PSDefault credential (Image Credit: Jeff Hicks)
advertisment
WMI error using a credential locally (Image Credit: Jeff Hicks)
Testing variable removal (Image Credit: Jeff Hicks)
The PSDefaultParameterValue setting (Image Credit: Jeff Hicks)
Overriding the parameter default value (Image Credit: Jeff Hicks)
advertisment
Using a null credential (Image Credit: Jeff Hicks)
$PSDefaultParameterValues.disabled = $True
Testing without the parameter default (Image Credit: Jeff Hicks)
Profile for current user all hosts (Image Credit: Jeff Hicks)
$PSDefaultParameterValues.Add("*:Credential",(Get-Credential globomantics\administrator))
I’m not even going to save the credential to a variable first. When I start a new PowerShell session, I’m prompted for the password.
Testing the profile (Image Credit: Jeff Hicks)
Testing the new default parameter credential (Image Credit: Jeff Hicks)
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri