Powershell notes

sum attribute of objects in array

( $foo.disksizerequired | Measure-Object -Sum ).Sum /1024/1024

Save down user photo from Active Directory

$path = "E:\test\output1.jpg"
$user = get-aduser "bgstack15" -properties thumbnailphoto

Display string of asterisks for a password. Obscure a password for display.

$output=""; For ($c=0; $c -lt $pass.length; $c++){$output+="*"}; Write-Host $output

Function uses -Verbose flag (from powershell – How to properly use the -verbose and -debug parameters in a custom cmdlet – Stack Overflow

function DoStuff {

    process {
        if ($PSBoundParameters['Verbose']) {
            # Do verbose stuff

        New-Item Test -Type Directory -Verbose:($PSBoundParameters['Verbose'] -eq $true)
DoStuff -Verbose

Also, this example from How to write a PowerShell function to use Confirm, Verbose and WhatIf | SQL DBA with A Beard

function Set-FileContent {
        [Parameter(Mandatory = $true)]
        [Parameter(Mandatory = $true)]
        [ValidateScript( {Test-Path $_ })]
    if ($PSCmdlet.ShouldProcess("$File" , "Adding $Content to ")) {
        Set-Content -Path $File -Value $Content


# Slightly more readable: 2020-01-15T17:07:59Z
#Also just fine
(Get-Date -Format FileDateTimeUniversal)

Creating a DateTime object with a specific UTC DateTime in PowerShell – Stack Overflow

Import CSV to dictionary

With column headings of ID and Data:

Import-Csv $filePath | % { $HashTable[$_.ID] = $_.Data }

Powershell CSV with headers to hashtable – Stack Overflow

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.