Managing Credentials Safely: A Guide for PowerShell Scripting

Managing Credentials Safely: A Guide for PowerShell Scripting

As system administrators, we rely on PowerShell scripts and Windows Scheduler to automate repetitive tasks. In most scenarios, we use shared servers to achieve this, which often requires embedding credentials directly in our scripts. From a security perspective, it’s essential to secure these credentials to prevent any risk of exposure. Properly securing credentials is key to maintaining the integrity of our systems and protecting sensitive information.

I came across a predefined script on a shared server that contained fully encrypted credentials. While this method hid the username and password, it had a significant flaw: anyone with access to that server could decrypt the credentials and misuse them. This highlights a crucial point for anyone concerned about security—encryption alone is not enough if access controls are not properly implemented. It’s essential to consider who can access these scripts and ensure robust security measures are in place. This article could be beneficial for those prioritizing security in their environments. In this approach, we store encrypted credentials in a file on the computer. The PowerShell script then uses the encrypted password from that file to create a credential object. This method allows us to securely handle credentials while keeping them separate from the script itself. Even if someone gains access to the encrypted file, they cannot decrypt the password; only the user who encrypted it can do so on the same server where it was encrypted.

Phase 1: Run the following steps once to create an encrypted password and store it in a text file for reference in the main script.

  • Log in with the user account you want to use to run the script and open PowerShell.

Article content

  • Run the following cmdlet to create a credential object and enter the username and password you want to use in your script when prompted.:

$GetCredential = Get-Credential

Article content

  • Encrypt the entered password and export it to a text file with the following command:

$GetCredential.Password | ConvertFrom-SecureString | Out-File EncryptedPassword.txt

Article content


Phase 2: Use the following steps in the main script to retrieve the encrypted password.

  • Convert the encrypted password back into a secure string with this command:

$SecurePassword = Get-Content .\EncryptedPassword.txt | ConvertTo-SecureString

Article content

  • Import the username from the credential object:

$Username = "<Your user name>"

Article content

  • Create a credential object to use in PowerCLI:

$MyCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword


Article content

  • Connect to your vCenter using the $MyCredential:

Connect-VIServer -Server <vCenter IP/FQDN> -Credential $MyCredential


Article content

  • Even if someone gains access to the encrypted file and opens the text file, they will not be able to recognize the password.

Article content

  • Additionally, they cannot use the encrypted password because it was specifically encrypted by User1 on that particular server.

Article content


To view or add a comment, sign in

More articles by Nader Heydari

Others also viewed

Explore content categories