~2 min read • Updated Dec 21, 2025
1. About Credentials
The -Credential parameter allows commands to run under specified accounts. Credentials can be provided as plain usernames (DOMAIN\User or Computer\User) or as PSCredential objects. Passwords are always prompted securely and masked.
2. Creating Credential Objects
- Get-Credential: Prompts for username and password, creating a PSCredential object.
- PSCredential: Stores username and password securely as a
SecureString. - GetNetworkCredential(): Method to retrieve plain-text values, valid only in the current session.
3. Using Credentials
- Credentials apply to all target computers in multi-computer commands.
- Profiles can store commonly used credentials for session reuse.
- Active Directory PSDrives inherit credentials used during mapping.
- Best practice: create credential objects before use rather than inline.
4. Advanced and Risky Techniques
- Packaging Scripts: Tools like PrimalScript can embed encrypted credentials.
- Saving Credential Objects: ConvertFrom-SecureString can persist passwords, but encryption is machine-specific.
- Custom PSCredential Creation: Use
Read-Hostfor console-only input without GUI prompts.
These methods introduce risks; passwords may be exposed if encryption keys are mishandled.
5. Supporting Credentials in Scripts
Scripts can include a -Credential parameter. If a PSCredential object is passed, it is used directly; if a string is passed, Get-Credential is invoked. This enables flexible authentication while maintaining security.
Conclusion
PowerShell Credentials provide secure authentication for administrative tasks. By leveraging PSCredential objects, SecureString, and best practices, administrators can enforce least privilege while avoiding insecure methods of storing or transmitting passwords.
Written & researched by Dr. Shahin Siami