PowerShell - Handling passwords.

 

Of course, passwords should never be stored in plain text in a script file. PowerShell offers possibilities for a secure handling of passwords. As an example, a password for a specific user can be stored encrypted as a text file. Only the respective user can decrypt the file, for other users the file is worthless.

Get-Credential

For entering username and password PowerShell provides the following cmdlet: Get-Credential

PS C:\Users\LiBe> $cred=Get-Credential

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential

Create PSCredential object and save it to a file

Some cmdlets can use the created PSCredential object directly. If the credentials are needed multiple times, they can of course be stored in a variable, or in a file for a later call:

#generate Authentication Passwordfile:
if (-not (Test-Path "$($env:Temp)\myapp_password.txt")) {
    $credential = Get-Credential
    $credential.Password | ConvertFrom-SecureString | Set-Content "$($env:Temp)\myapp_password.txt"
}

Read PSCredential object from file

To create the PSCredential object from a file we need the user name: Variable $user

$Credential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, (Get-Content "$($env:Temp)\myapp_password.txt" | ConvertTo-SecureString)

Save password to a file

The password can also be saved directly to a file without "Get-Credential".

"password" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "$($env:Temp)\myApp_password.txt"

The password "password" looks like this in the file:

Read password from the file and use it as plain text password:

$SecureCredential = Get-Content "$($env:Temp)\myApp_password.txt" | ConvertTo-SecureString
$UnsecurePassword = (New-Object PSCredential "username",$SecureCredential).GetNetworkCredential().Password
positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Publication: 2022-05-02 from Bernhard | Übersetzung Deutsch |🔔 | Comments:0

PowerShell: File attributes: Change date - without any tools. | PowerShell | PowerShell read Eventlog

Top articles in this section


PowerShell Log-Files: Logging into a textfile - write to file

Log files in PowerShell can be created via the Out-File command, via a custom function, or via PowerShell's built-in Transcript.


PowerShell: Prevent screen saver, lock: Move mouse regularly

Alternatively, if you can't change the screen lock settings, you can move the mouse regularly, or have a script move the mouse. Originally published as an AutoIt script, I recreated the script with a few PowerShell lines. Anyone who copies the following commands into a PowerShell session will prevent the computer from locking the screen or starting the screensaver:


PowerShell: File attributes: Change date - without any tools.

As an alternative to special programs, the date of a file or folder can also be changed with PowerShell. 

Questions / Comments