Skip to content

Functions

This page contains PowerShell functions you might find useful.

Windows Administration

- Enable remote desktop (source: PowerShell blog and here). Does not work for Vista :-).

Function Enable-RDP ($Server)
{
$Terminal = Get-WmiObject Win32_Terminal –Computer $Server
$Terminal.Enable($True)
}

- Run as

Function Run-as
{
$cred = get-credential
[System.Diagnostics.Process]::start($args[0], $null, $cred.UserName, $cred.password, $null)
}

- Convert a SecureString to cleartext. Useful if you need to pass the cleartext password to other programs that do not accept a SecureString (source).

function Convert-SecureString($sstring)
{
 $BSTR=[System.Runtime.InteropServices.marshal]::SecureStringToBSTR($sstring)
 $ClearString=[System.Runtime.InteropServices.marshal]::PtrToStringAuto($BSTR)
 [System.Runtime.InteropServices.marshal]::ZeroFreeBSTR($BSTR)
 $ClearString
}

XML

- Read an xml file and return xml type

function get-xml($path)
{
return [xml] [string]::join(”`n”, (gc $path))
}

User interaction

- Pause and wait for a key (source: PowerShell blog)

function Pause ($Message=”Press any key to continue…”)
{
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey(”NoEcho,IncludeKeyDown”)
Write-Host “”
}

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*