Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


I am new to Windows admin-type stuff but I am familiar with Unix/Linux, etc. I have a simple PowerShell 1 script that should only copy files that match this naming convention:


My pattern in $source is causing errors: "Could not find part of the path.." Need to know how to do pattern matching in the PowerShell environment I guess

Here is my code:

$source = "C:\Users\Tom\Daily_Reviews\[\d\d\d\d\-\d\d\d\d\]\.journal" 
$destination ="C:\Users\Tom\Processed\"

if(-not(Test-Path $destination)){mkdir $destination | out-null}

  Copy-Item $source $destination
share|improve this question
Can you clarify, PS1 is the extension, but the host may not be version 1 -- What's the output of $PSVersionTable? – jscott Apr 10 '13 at 15:47
That variable doesn't exist. I also personally installed PowerShell v1 today. There was nothing on this machine prior so I am pretty sure it's version1 – Slinky Apr 10 '13 at 15:58
As an fyi these type of questions might be better answered in stackoverflow. – tony roth Apr 10 '13 at 17:41
up vote 1 down vote accepted

You can use -match for instance if ( "$source" -match "Daily_Reviews*" ) { echo Match! }

This accepts regex.

What you are trying to do should probably be done with a for loop anyhow, like for example

ls Daily_Reviews* > file.txt ; get-content file.txt while read input, do a copy, etc...

(I'm not a powershell guy)

share|improve this answer

You might try something more like this:

$source = Join-Path "C:\Users" $env:username
$destination = Join-Path $source "Processed"

if (![System.IO.Directory]::Exists($destination)) {[System.IO.Directory]::CreateDirectory($destination)}

dir -Path $source -file -filter "*.journal" | where { $_ -match "Daily_Reviews\d{4}" } | ForEach-Object { Move-Item -Path $_.fullname -Destination $destination }

However, this might not work in PS1. It might be worth upgrading to Powershell 2?

This was assuming that you were looking to transfer more than one file - you could have equally populated an array with the different potential names of files, and checked each individually, but creating the variable as you did won't work (to my knowledge).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.