PowerShell - Moving files from one server to the next when the filename changes

I am trying to copy multiple files, which name changes from day to day, from one machine to the next and am having issues. The names always starts with "Stock Status" and then ends with numbers.

I have tried the following code:

$strSourceFile = "C:\Test\[Stock]*"
$strTargetDir = "C$\Test\Test2"
$astrComputerList = ( "kburrows-xplt" )

if ([System.IO.File]::Exists($strSourceFile)) 
    foreach ($strComputer in $astrComputerList) {
        $strTargetPath = "\\$strComputer\$strTargetDir"
        copy-item $strSourceFile -destination $strTargetPath

The problem with this is that strSourceFile is set to a string, but I think it needs to be an expression so this would work.

Does anyone know how to do this? Maybe I am going about it all wrong.

  Directory: C:\Test

Mode                LastWriteTime     Length Name                                                                                                                          
----                -------------     ------ ----                                                                                                                          
-a---         11/1/2013   3:09 AM    2954557 Stock Status Report88744.XML                                                                                                  
-a---         11/1/2013   3:25 AM     528934 Stock Status Report89386.XML                                                                                                  
-a---         11/1/2013   3:31 AM     103583 Stock Status Report89772.XML       


Here is what I would recommend for this operation:

$strSourceFile = "C:\Test\"
$strTargetDir = "C$\Test\Test2"
$astrComputerList = ( "kburrows-xplt" )

Get-ChildItem $strSourceFile -Filter "Stock Status*" | foreach{
  if (Test-Path -Path $_.FullName -PathType Leaf) 
    foreach ($strComputer in $astrComputerList) {
        $strTargetPath = "\\$strComputer\$strTargetDir"
        copy-item $_.FullName -destination $strTargetPath

$_ is the current object in the pipeline. Similar to "this" in other languages. The -Filter allows you trim down the results as needed. I believe -Include and -Exclude allow you to filter based on extension as well if that helps.

I as well don't have 1.0 available so I can't fully test.

Try removing the square brackets around Stock in your first line. You can use wildcards in a string as part of Copy-Item.

Also, you can replace



Test-File $strSourceFile

I just noticed that you tagged this as PowerShell 1.0, so perhaps the wildcard doesn't work with Copy-Item on that version. In that case, try the following:

Get-ChildItem $strSourceFile | Copy-Item -Destination $strTargetPath

Unfortunately, I don't have a system with PowerShell 1.0 handy to test this with.

Need Your Help

Check if is an attribute is a valid DOM attribute

jquery html dom

I've noticed that if you take an element with jQuery you can change every attribute with the attr method. That's what supposed to do right?

Why UILabel in UINavigation is not centered

ios iphone objective-c

I use custom back btn in navigation controller: