Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / PowerShell SecretManagement Module: Securely Manage Credentials and Secrets

May 10, 2023 PowerShellWindows 10Windows Server 2019

PowerShell SecretManagement Module: Securely Manage Credentials and Secrets

Microsoft recently released a very useful SecretManagement PowerShell module. You can use it to securely store and use saved passwords (secrets) in your PowerShell scripts. The module consists of two components: a SecretStore vault (a default password store), and a SecretManagement (engine to access different password vaults). Both the built-in SecretStore vault and third-party secret vaults (like KeePass, LastPass, HashiCorp Vault, Azure Key Vault, Bitwarden, Windows Credential Manager, etc.) are supported. Using SecretManagement, you can save any passwords (credentials) to the secret vault and retrieve them at any time. You can also store license keys, access keys, and other sensitive information (Hashtable, Byte, String, SecureString, and PSCredential object types are supported).

In this article, we’ll show how to use the SecretManagement module in your PowerShell scripts to store and retrieve credentials, as well as an example of KeePass integration.

Contents:
  • Installation of the Secret Management Module
  • Create a Password Store (SecretStore Vault) via PowerShell
  • Managing Saved Credentials Using Secret Management Module
  • Using Saved Passwords from Secret Vault in PowerShell Scripts
  • Managing KeePass Passwords and Secrets with PowerShell

Installation of the Secret Management Module

The SecretManagement module requires Windows PowerShell version 5.1 or PowerShell Core 6.x, 7.x.

To install the SecretManagement using the NuGet package manager, run the command below:

Install-Module -Name Microsoft.PowerShell.SecretManagement

To install the default SecretStore vault offered by Microsoft, run the following command:

Install-Module -Name Microsoft.PowerShell.SecretStore

install Microsoft PowerShell.SecretManagement module

To display a list of available cmdlets in the module, use these commands:

Get-Command -Module Microsoft.PowerShell.SecretManagement
Get-Command -Module Microsoft.PowerShell.SecretStore

get commamds from PowerShell.SecretManagement

Create a Password Store (SecretStore Vault) via PowerShell

First of all, create a local secret vault. I will name it MyDomainPassdb and make it a default password store.

Register-SecretVault -Name MyDomainPassdb -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault

If the settings of your PowerShell script execution policy prevent the module from running, you can change the settings for your current session as follows:

Set-ExecutionPolicy -Scope Process Unrestricted

You can create and use both local and remote password vaults.

The command below displays a list of registered password vaults for the current user:

Get-SecretVault

create local SecretStore vault

Create a master password to access your SecretStore Vault:

Get-SecretStoreConfiguration

If you forget your SecretStore Vault master password, you won’t be able to access the data stored in it.

By default, the following settings determine who and how can access password stores:

  • Scope – CurrentUser (only the current user can access the SecretStore)
  • Authentication – Password (access vault using a master password)
  • PasswordTimeout – 900 – the duration of the session (in seconds) during which you don’t need to re-enter your master password, You can extend a session length: Set-SecretStoreConfiguration -PasswordTimeout 1200
  • Interaction – Prompt (whether to enter the master password when making changes)

Get-SecretStoreConfiguration powershell

To disable the request for a master password to access the secret vault (not recommended), set Authentication = None:

Set-SecretStoreConfiguration -Authentication None

To change the master password, use the Set-SecretStorePassword cmdlet.

In Windows, a local password store is located in the user profile folder %LOCALAPPDATA%\Microsoft\PowerShell\secretmanagement.

secretmanagement secretvault in user profile directory

Unfortunately, you cannot use the Secret Management module for Managed Service Accounts (MSA/gMSA) since no profiles are created for them.

Managing Saved Credentials Using Secret Management Module

The Set-Secret cmdlet is used to add a secret of type SecureString to the password vault. Specify the vault name and the entry name:

Set-Secret -Vault MyDomainPassdb -Name user1

Enter the password (secret) you want to save in the store.

Or, you can save a protected value as follows (e. g., a GitHub key):
Set-Secret -Vault MyDomainPassdb -Name MY_GITHUB_TOKEN -Secret 'GitHub_AUTH_API_Token'

Be attentive when entering private information as plain text in the PowerShell console, since it is saved in the PowerShell command history.

You can display a list of entries in the secret vault:

Get-SecretInfo

Get-SecretInfo from secret store

In PowerShell 7.x, you can display a protected value from the password vault as plain text using –AsPlainText option (update your PowerShell version if needed):

Get-Secret -Vault MyDomainPassdb -Name user1| ConvertFrom-SecureString –AsPlainText

In most cases, you have to save both a username and a password instead of saving a password only when working in Windows networks. In this case, save the credentials as a PSCredential object. You can also add metadata with the description of the saved entry.

Set-Secret -Vault MyDomainPassdb -Name adm_maxbak -Secret (Get-Credential) -Metadata @{description = "AD enterprise admin woshub.com"}

If you don’t want to enter an account name in the Get-Credential window, you can specify it like this:

Set-Secret -Vault MyDomainPassdb -name adm_maxbak -Secret (get-credential woshub\adm_maxbak)

Here is how you can display a list of saved passwords and their descriptions:

Get-SecretInfo | Ft Name, Metadata

get credentials from secret vault using powershell

Using Saved Passwords from Secret Vault in PowerShell Scripts

Now you can use saved passwords in your PowerShell scripts and commands. For example, one of my customers is using a dozen of accounts for each administrator and different services/tasks for security and administrative account protection reasons. Using the same passwords is forbidden, passwords are regularly audited. Administrators find it tedious to constantly enter different passwords.

Using the SecretManagement module, you can safely store your passwords in a local vault and get them if necessary.

For example, to connect to a remote computer and run a command through PowerShell Remoting, you can use the following code:

Enter-PSSession -ComputerName mun-dc01 -Credential (Get-Secret -Vault MyDomainPassdb -Name adm_maxbak)

In the same way, you can connect Exchange or Microsoft 365 (ex-Office 365) easier:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mun-exch1.woshub.com/PowerShell/ -Authentication Kerberos -Credential (Get-Secret -Vault MyDomainPassdb -Name adm_ex_maxbak)

Or connect your Azure AD tenant:

Connect-AzureAD -Credential (Get-Secret -Vault MyDomainPassdb -Name azadm_ maxbak)

Or just get the credentials and store them in a PowerShell variable:

$Cred = Get-Secret -Vault MyDomainPassdb user1

Managing KeePass Passwords and Secrets with PowerShell

You can use the SecretManagement module to access other popular password vaults. Let’s see how to access saved passwords in a KeePass file (*.kdbx).

First of all, install the SecretManagement module to interact with KeePass:

Install-Module -Name SecretManagement.KeePass

installing SecretManagement.KeePass module

Then register the KeePass vault file located in your user profile:

Register-SecretVault -Name "KeePassDB" -ModuleName "SecretManagement.Keepass" -VaultParameters @{
Path = "C:\Users\maxbak\Documents\personal_creds.kdbx"
UseMasterPassword = $true
}

To check access to a KeePass file, run the command:

Test-SecretVault -Name KeePassDB

accessing keepass vault using master password via powershell

Enter the master password to access the KeePass vault. If you have entered the password correctly, the command returns True.

Then display a list of saved passwords in the KeePass database:

Get-SecretInfo -Vault KeePassDB

Get-SecretInfo - getting secret from keepass database

To save a new secret in KeePass:

Set-Secret -Vault KeePassDB -Name "ILO_adm" -Secret (Get-Credential woshub\ILO_adm)

In the same way, you can connect any other popular password store solution and use it in PowerShell.

0 comment
0
Facebook Twitter Google + Pinterest
previous post
PowerShell: Get Folder Sizes on Disk in Windows
next post
How to Manually Import (Add) Update into WSUS from Microsoft Update Catalog?

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

November 7, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • Adding Drivers into VMWare ESXi Installation Image
  • How to Hide Installed Programs in Windows 10 and 11
  • Configuring SFTP (SSH FTP) Server on Windows
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top