# How do I specify default PSCredential for my script?

**URL:** <https://forums.powershell.org/t/how-do-i-specify-default-pscredential-for-my-script/2277>\
**Category:** PowerShell Help\
**Created:** [February 20, 2014, 7:50am UTC](https://forums.powershell.org/t/how-do-i-specify-default-pscredential-for-my-script/2277 "2014-02-20T07:50:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregory-suvalian](https://avatars.discourse-cdn.com/v4/letter/g/9d8465/32.png) [@gregory-suvalian](https://forums.powershell.org/u/gregory-suvalian)\
**Post date:** [February 20, 2014, 7:50am UTC](https://forums.powershell.org/t/how-do-i-specify-default-pscredential-for-my-script/2277/1 "2014-02-20T07:50:11Z")

</div>

Hello,

I want my script to access PSCredential type parameter which is optional. I want to use credentials of current user if it’s not supplied but use $cred if it’s supplied and I plan to use that parameter to use later say in Get-WMIObject calls.  
How do I properly initialize $cred parameter?

[Parameter(Mandatory=$False)]  
[PSCredential]$cred;

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [February 20, 2014, 8:03am UTC](https://forums.powershell.org/t/how-do-i-specify-default-pscredential-for-my-script/2277/2 "2014-02-20T08:03:31Z")

</div>

Splatting can be helpful here:

```
[CmdletBinding()]
param (
    [pscredential]
    $Credential
)

# Setting up the hashtable which will either be empty, or contain your $Credential object

$credHash = @{}
if ($Credential)
{
    $credHash['Credential'] = $Credential
}

# Splatting the hashtable to other commands. If $credHash contains a credential object, it gets
# passed to the cmdlet. If it's empty, nothing is passed to the -Credential parameter, and Get-WmiObject
# defaults to using the current user's credentials.

Get-WmiObject -Class Win32_OperatingSystem @credHash
```

I’ve read that you can also set your parameter’s default value to [pscredential]::Empty , but that some cmdlets don’t properly handle that value. It’s more reliable to use the splatting approach.

---

<div class="post-metadata">

**Author:** ![rob-campbell](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@rob-campbell](https://forums.powershell.org/u/rob-campbell)\
**Post date:** [February 20, 2014, 8:55am UTC](https://forums.powershell.org/t/how-do-i-specify-default-pscredential-for-my-script/2277/3 "2014-02-20T08:55:28Z")

</div>

Another option:

`[CmdletBinding()]
param (
[pscredential]
$Credential
)
if ($Credential)
{
$PSDefaultParameterValues = $PSDefaultParameterValues.clone()
$PSDefaultParameterValues[‘*:Credential’] = $Credential
}`

Now any cmdlet that takes a -Credential parameter will automatically use that credential, and no other changes are required in the script.

Cloning the $PSDefaultParameterValues is necessary to preserve whatever default parameters values are set in the session, while protecting the preference variable in the parent scope. Unlike the other preference variables, it’s a hash table, which means just adding a new key would actually update the preference in the parent scope, and would still be in effect when the script exits.

---

<div class="post-metadata">

**Author:** ![gregory-suvalian](https://avatars.discourse-cdn.com/v4/letter/g/9d8465/32.png) [@gregory-suvalian](https://forums.powershell.org/u/gregory-suvalian)\
**Post date:** [February 21, 2014, 3:21am UTC](https://forums.powershell.org/t/how-do-i-specify-default-pscredential-for-my-script/2277/4 "2014-02-21T03:21:10Z")

</div>

You guys are awesome!

What notations in '_:Credential’ means in brackets? I assume brackets are used to specify which value you need to pull out of hashtable based on that key. But both "_" and “:” I assume signify something special

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [February 21, 2014, 3:37am UTC](https://forums.powershell.org/t/how-do-i-specify-default-pscredential-for-my-script/2277/5 "2014-02-21T03:37:00Z")

</div>

The complete details of that variable are in the about\_Parameters\_Default\_Values help file. It’s a hashtable / Dictionary with a special syntax enforced for keys. The keys must be “\<Command\>:\<ParameterName\>”; the \<Command\> part can be \*, in which case it applies to all commands with \<ParameterName\>.

You do need to be running at least PowerShell 3.0 to use $PSDefaultParameterValues, though; keep that in mind if you’ve still got older versions floating around.

---

<div class="post-metadata">

**Author:** ![gregory-suvalian](https://avatars.discourse-cdn.com/v4/letter/g/9d8465/32.png) [@gregory-suvalian](https://forums.powershell.org/u/gregory-suvalian)\
**Post date:** [February 24, 2014, 4:15am UTC](https://forums.powershell.org/t/how-do-i-specify-default-pscredential-for-my-script/2277/6 "2014-02-24T04:15:12Z")

</div>

Hello,

Got following error when using substitution method on following command. How do I remove credentials when specifically using Add-Content? Issue is that credentials which I specify manually when I run cmdLet is network credentials to query AD in different domain but I have to write to local file system where that account do not have permissions. Is it still possible to use substituion method instead of creating HastTable?

**Add-Content -Path @“c:\out.txt” -Value (“Processing account”)**

`
The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again without specifying credentials.
At C:\ExpirationMailer.ps1:78 char:1
+ Add-Content -Path $OutputFile -Value ("Processing account $_.givenName $_.Surnam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotImplemented: (:) [], PSNotSupportedException
    + FullyQualifiedErrorId : NotSupported
`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [February 24, 2014, 4:20am UTC](https://forums.powershell.org/t/how-do-i-specify-default-pscredential-for-my-script/2277/7 "2014-02-24T04:20:26Z")

</div>

You just have to be more selective in which cmdlets the substitution applies to. A key of ‘\*:Credential’ means your PSCredential gets applied to any cmdlet which has a “Credential” parameter. You could do something like this, instead:

```
$PSDefaultParameterValues['*-AD*:Credential'] = $Credential
```

That would only apply to cmdlets whose name contained “-AD”. You can make multiple entries to the table, if you need to use some cmdlets and not others.

Personally, I prefer the splatting / hashtable approach, for its PowerShell 2.0 compatibility and finer control in situations like this.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:52pm UTC](https://forums.powershell.org/t/how-do-i-specify-default-pscredential-for-my-script/2277/8 "2024-05-16T20:52:22Z")

</div>

Closed on May 16, 2024, 8:52pm UTC


