# Move-item 2 issues

**URL:** <https://forums.powershell.org/t/move-item-2-issues/16234>\
**Category:** PowerShell Help\
**Created:** [April 12, 2021, 6:02pm UTC](https://forums.powershell.org/t/move-item-2-issues/16234 "2021-04-12T18:02:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevins](https://avatars.discourse-cdn.com/v4/letter/k/ee59a6/32.png) [@kevins](https://forums.powershell.org/u/kevins)\
**Post date:** [April 12, 2021, 6:02pm UTC](https://forums.powershell.org/t/move-item-2-issues/16234/1 "2021-04-12T18:02:37Z")

</div>

I am trying to move certain files from c:\ on a list of servers to a central location. I have 2 problems.

1. Apparently I don’t have access to all of them (and I’m not sure which ones). I tried to use write-output but couldn’t get the server name.
2. Some of the files are still being written to and are locked. (ideally I would just skip those. I tried to use try / catch but couldn’t get the syntax to work. Here is my code:

```auto
$file = import-csv -LiteralPath "F:\auditFilesFromLocalServers\auditserverList.csv" 

foreach($i in $file)
{
         If (-not (Move-Item -Path "\\$($i.name)\c`$\auditLogs\*.sqlaudit" -Destination "\\myCentralServer\F`$\auditFilesFromLocalServers" ))
         {
    Throw "Folder Access Denied" # and this causes NONE of the servers to transfer files. 
         }
}

```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [April 12, 2021, 7:40pm UTC](https://forums.powershell.org/t/move-item-2-issues/16234/2 "2021-04-12T19:40:42Z")

</div>

The files need to be processed be each to track what happens for each file. The following is not tested, but should be fairly close:

```auto
$csv = import-csv -LiteralPath "F:\auditFilesFromLocalServers\auditserverList.csv" 

$results = foreach($row in $csv) {
    $source = '\\{0}\c$\auditLogs\*.sqlaudit' -f $row.Name    
    $destination = '\\myCentralServer\F`$\auditFilesFromLocalServers'
    
    $files = Get-ChildItem -Path $source
    
    foreach ($file in $files) {
        try {
            Move-Item -Path $file.FullName -Destination $destination -ErrorAction Stop 
            [PSCustomObject]@{
                ComputerName = $row.Name
                SourcePath = $source
                Source = $file.FullName
                Destination = $destination
                Status = 'SUCCESS'
            }
        }
        catch {
            [PSCustomObject]@{
                ComputerName = $row.Name
                SourcePath = $source
                Source = $file.FullName
                Destination = $destination
                Status = 'FAILED: {0}' -f $_
            }        
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [April 12, 2021, 9:06pm UTC](https://forums.powershell.org/t/move-item-2-issues/16234/3 "2021-04-12T21:06:20Z")

</div>

Rob’s code removing some redundant parts. Still not tested

```auto
$csv = import-csv -LiteralPath "F:\auditFilesFromLocalServers\auditserverList.csv" 

$results = foreach($row in $csv) {
    $source = '\\{0}\c$\auditLogs\*.sqlaudit' -f $row.Name    
    $destination = '\\myCentralServer\F`$\auditFilesFromLocalServers'
    
    $files = Get-ChildItem -Path $source
    
    foreach ($file in $files) {

        try {
            Move-Item -Path $file.FullName -Destination $destination -ErrorAction Stop 
            $status = 'SUCCESS'
        }
        catch {
            $status = 'FAILED: {0}' -f $_
        }

        [PSCustomObject]@{
            ComputerName = $row.Name
            SourcePath = $source
            Source = $file.FullName
            Destination = $destination
            Status = $status
        } 
    }
}

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:26pm UTC](https://forums.powershell.org/t/move-item-2-issues/16234/4 "2024-05-16T20:26:49Z")

</div>

Closed on May 16, 2024, 8:26pm UTC


