Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

Hashtable

tags: PowerShell categories: PowerShell-Basic

Table of Contents

Create a hashtable

Create a hashtable from an file-system-object.

Get-ChildItem -Path $home | ForEach-Object{
    @{$_.Name=(Split-Path -Parent $_.Fullname)}
}

Output from my Mac:

Name                           Value
----                           -----
Applications                   /Users/xxx
Desktop                        /Users/xxx
Documents                      /Users/xxx
Downloads                      /Users/xxx
Movies                         /Users/xxx
Music                          /Users/xxx
Pictures                       /Users/xxx
Public                         /Users/xxx

Loop through a hashtable

Loop through a hashtable and get one single entry.

$targetroot = 'Z:'
$datahash   = [ordered]@{
    'S:\Scripting'    = "$($targetroot)\Backup\Scripting"
    'D:\Dokumente'    = "$($targetroot)\Backup\Dokumente"
    'W:\WebSites'     = "$($targetroot)\Backup\WebSites"
    'C:\Daten\Tools'  = "$($targetroot)\Backup\Tools"
    'F:\Fotografie'   = "$($targetroot)\Fotografie\Kataloge"
    'H:\HyperV'       = "$($targetroot)\Virtualisierung\HyperV"
}
foreach($item in $datahash.keys){
    if($item -match 'WebSites'){
        Write-Output "Source $($item), Target $($datahash[$item])"
    }
}

Output:

Source W:\WebSites, Target Z:\Backup\WebSites

[ Top ] [ Blog ]