Copy files with source and destination hashtables

Welcome Forums General PowerShell Q&A Copy files with source and destination hashtables

This topic contains 3 replies, has 3 voices, and was last updated by

3 months, 2 weeks ago.

  • Author
  • #132101

    Points: 17
    Rank: Member

    I'm writing a backup utility to use when changing out computers, and I am using hash tables for the source and destination locations.  I cannot figure out how to set it up to loop through the source locations and reference the destination locations.  For example, a $Locations hashtable, which contains the directory name and the corresponding path (e.g. "Data" = "C:\Data"), and a $Destinations hashtable that represents the backup locations that is built from user responses (e.g. "Data" = "D:\Data").

    If I use the following code, it gives me the source locations like I want, but I don't know how to reference the corresponding value in the destination hashtable.

    ForEach ($Source in $Locations.Values) {
    Write-Host $Source

    If I leave off the .Values and put it in the write statement like this:

    ForEach ($Source in $Locations) {
    Write-Host $Source.Values

    in an attempt to make it more generic, I get all the items in one big line, (e.g. C:\Data C:\Users\\Downloads C:\Users\\My Documents).  Of course there is very likely a better way to do this, so I am open to suggestions.  Thanks for your assistance.

  • #132431

    Points: 411
    Helping Hand
    Rank: Contributor

    Hi Mike,

    You can use the key/name reference with the hashtable...

    PS C:\> [hashtable] $Source = @{ Data = 'C:\Data'; Files = 'C:\Files' }
    PS C:\> $Source
    Name                     Value
    —-                          —–
    Data                      C:\Data
    Files                      C:\Files
    PS C:\> $Source.Data
    PS C:\> $Source['Files']
  • #132435

    Points: 639
    Helping Hand
    Rank: Major Contributor

    You probably want a PSObject versus a hashtable.

    $map = @()
    $map += [pscustomobject]@{
        Source = 'C:\Data'
        Destination = 'D:\Data'
    $map += [pscustomobject]@{
        Source = 'C:\Scripts'
        Destination = 'D:\Scripts'
    foreach ($path in $map) {
        'Moving {0} to {1}' -f $path.Source,$path.Destination

    The $map could be a CSV or manually mapped like the example above. With that in mind, would a destination be a different path than the source? Traditionally, I would think you would want something more like this:

    $paths = 'C:\Data',
    $destination = '\\SomeServer\SomeShare\{0}' -f $env:COMPUTERNAME
    foreach ($path in $paths) {
        'Moving {0} to {1}' -f $path,$destination
  • #132443

    Points: 17
    Rank: Member

    This is an accidental duplicate of my original post.  The solution to the process ended up being using the keys property of the hashtable, not the values property.  That way I was able to use the same reference (e.g. $Location["PSTs"] and $Destination["PSTs"), and it worked out great.  As usual, I spent a couple of days working on the problem, and then right after I ask for assistance, I stumble upon my answer.  Thanks for the suggestions.

The topic ‘Copy files with source and destination hashtables’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort