Powershell notes

sum attribute of objects in array

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

Save down user photo from Active Directory

https://devblogs.microsoft.com/scripting/weekend-scripter-exporting-and-importing-photos-in-active-directory/
$path = "E:\test\output1.jpg"
$user = get-aduser "bgstack15" -properties thumbnailphoto
[System.Io.File]::WriteAllBytes($path,$user.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 {
    [CmdletBinding()]
    param()

    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 {
    [cmdletbinding(SupportsShouldProcess)]
    Param(
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Content,
        [Parameter(Mandatory = $true)]
        [ValidateScript( {Test-Path $_ })]
        [string]$File
    )
    if ($PSCmdlet.ShouldProcess("$File" , "Adding $Content to ")) {
        Set-Content -Path $File -Value $Content
    }
}

Timestamps

# Slightly more readable: 2020-01-15T17:07:59Z
(Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
#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.