Store password as encrypted string in a file
- Uses Windows Data Protection API (DPAPI), so that only the same user on the same machine can decrypt it.
$passwordFile = "C:\scripts\encryptedPassword.dat"
$securedPassword = Read-Host "Enter password" -AsSecureString
$securedPassword | ConvertFrom-SecureString | Out-File $passwordFile
Mount network share as network drive
$networkShare = "\\my-server\data"
$username = "my-server\utility-user"
$passwordFile = "C:\scripts\encryptedPassword.dat"
$securedPassword = Get-Content $passwordFile | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PSCredential(
$username, $securedPassword)
New-PSDrive -Name "Q" -PSProvider FileSystem -Root $CONFIG.DestinationShare -Credential $credential -Persist
Test-Path "Q:\"