Get user credentials securely in PowerShell

#Get user credentials securely
Try {
	$credentials = Get-Credential "$env:username"
}
Catch {
	$log += "`n- " + $_.Exception.Message
	Output-Result
}

#Get current domain to use for authentication. ADSI = Active Directory
$currentDomain = "LDAP://" + ([ADSI]"").distinguishedName

#Authenticate
$activeDirectoryEntry = New-Object System.DirectoryServices.DirectoryEntry($currentDomain,$credentials.GetNetworkCredential().UserName,$credentials.GetNetworkCredential().Password)
if ($activeDirectoryEntry.name -eq $null)
{
    $log += "`n- Failed to authenticate that username and password."
    Output-Result	
} else {
    $log += "`n- Authentication was successful."
}

#Display Results
$log = "Results:"
function Output-Result {
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") > null
    $oReturn=[System.Windows.Forms.Messagebox]::Show($log)
    Break
}

Fix “is not digitally signed” powershell error

#Open powershell as administrator
Get-ExecutionPolicy -List
         Scope               ExecutionPolicy
         -----               ---------------
 MachinePolicy               Undefined
    UserPolicy               Undefined
       Process               Unrestricted
   CurrentUser               Unrestricted
  LocalMachine               Unrestricted

#Set all to Unrestricted where allowed. The first two are usually managed by group policy.

Set-ExecutionPolicy Unrestricted -Scope Process
#type Y and press enter