Instead of hard code, the switch statement, I would probably build a hash table from a text file containing pairs of key values; this would mean that anyone new to Powershell could administer the file name / destination relationship. I'm not sure if this will be more efficient, but that means you do not need to update the script if and when the file or destination names change.
Here is a quick example ... it does not do any copying but demonstrates a method:
$hashData = ConvertFrom-StringData ([IO.File]::ReadAllText("c:\temp\_sotemp\_hash\hashfile.txt")) $directory = 'C:\Temp\_sotemp' Get-ChildItem $directory | where {!($_.PsIsContainer)} | Foreach-Object { Foreach ($key in $hashData.GetEnumerator()){ if ($_.name.substring(0,7) -eq $key.Name){ Write-Host $_.fullname " will be copied to: " $key.Value } } }
A few comments. First, do not use CMDLet Get-Content to read a text file containing key value pairs, as it can do some strange things for hash tables - you can get hash hashes! Secondly, the substring method will throw an error if you pass a file name with less than 7 characters - can you handle this?
Here is the contents of the text file:
geofile=c:\\temp\\_sotemp\\REGIONPROD other_f=c:\\temp\\_sotemp\\SSBRPRD alter_f=c:\\temp\\_sotemp\\SSBRPRD some_fi=c:\\temp\\_sotemp\\SMPROD
source share