OpsaC - Operating as PowerShell code
published: February 12, 2019 author: Tinu tags: PowerShell categories: PowerShell-Basic
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 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