If you’re working with PowerShell scripts, you often need to handle credentials securely. This is a very common requirement for PowerShell developers. In this tutorial, I will explain various methods to add credentials in PowerShell scripts with examples.
To add credentials in a PowerShell script using the Get-Credential cmdlet, simply use the command $credential = Get-Credential. This will prompt the user to enter their username and password, creating a credential object. You can then use this object to authenticate commands, such as connecting to a remote server with Enter-PSSession -ComputerName ServerUSA01 -Credential $credential. This method ensures that credentials are securely handled within your script.
PowerShell Credentials
In PowerShell, credentials are typically handled using the Get-Credential cmdlet, which prompts the user for a username and password and returns a credential object. This object can then be used in various cmdlets that require authentication.
Basic Syntax
The basic syntax for obtaining a credential object in PowerShell is:
$credential = Get-CredentialA dialog box appears when this command is executed, prompting the user to enter their username and password.
Check out PowerShell Where-Object
Add Credentials in PowerShell Script
Now, let me show you different methods to add credentials to a PowerShell script.
Method 1: Using Get-Credential
The Get-Credential cmdlet is the most used cmdlet to prompt for and use credentials in your PowerShell script.
Let me show you an example.
Example: Connect to a Remote Server
Suppose you need to connect to a remote server named ServerUSA01:
$credential = Get-Credential
Enter-PSSession -ComputerName ServerUSA01 -Credential $credentialIn this example, the Enter-PSSession cmdlet uses the credential object to authenticate the user on the remote server.
Method 2: Store Credentials Securely
While Get-Credential is handy, it requires user interaction. For automation, you might want to store credentials securely and retrieve them when needed. You can use the Export-Clixml and Import-Clixml cmdlets to achieve this.
Example: Exporting and Importing Credentials
Here is the complete script for exporting and importing credentials using PowerShell.
Exporting Credentials:
$credential = Get-Credential
$credential | Export-Clixml -Path "C:\secure\credential.xml"This command saves the credential object to an XML file.
Importing Credentials:
$credential = Import-Clixml -Path "C:\secure\credential.xml"This command retrieves the credential object from the XML file, allowing you to use it in your scripts without user interaction.
Example: Automating a Task with Stored Credentials
Let’s automate a task to restart a remote service on ServerUSA02:
$credential = Import-Clixml -Path "C:\secure\credential.xml"
Invoke-Command -ComputerName ServerUSA02 -Credential $credential -ScriptBlock {
Restart-Service -Name "Spooler"
}Check out PowerShell Naming Conventions & Best Practices
Method 3: Using Secure Strings
You can use secure strings in PowerShell for scenarios where you need to hard-code credentials (not recommended for production).
Let me show you an example.
Example: Create a Credential Object from a Secure String
$username = "adminUSA"
$password = ConvertTo-SecureString "P@ssw0rd!" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $password)In this example, the password is converted to a secure string and then combined with the username to create a credential object.
You can see the output in the screenshot below:

Method 4: Pass Credentials to a Function
If you’re writing a PowerShell function that requires credentials, you can add a -Credential parameter.
Let me show you an example to help you understand it.
Example: Function with Credential Parameter
Here is the complete PowerShell script.
function Restart-RemoteService {
param (
[string]$ComputerName,
[string]$ServiceName,
[PSCredential]$Credential
)
Invoke-Command -ComputerName $ComputerName -Credential $Credential -ScriptBlock {
param ($ServiceName)
Restart-Service -Name $ServiceName
} -ArgumentList $ServiceName
}
$credential = Get-Credential
Restart-RemoteService -ComputerName "ServerUSA03" -ServiceName "W32Time" -Credential $credentialIn this function, the -Credential parameter allows you to pass a credential object when calling the function.
Conclusion
In this tutorial, I explained how to add credentials to a PowerShell script using different methods. For interactive prompts in PowerShell, I recommend using the Get-Credential method. You can also use the Secure Strings method to hard-code credentials in a PowerShell script.
You may also like:
- Encrypt a File with a Password in PowerShell
- How to Check PowerShell Version?
- PowerShell Do-Until Loop Examples
- PowerShell Data Types
- How to Wait for a Command to Finish in PowerShell?
- How to Set Execution Policy in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.