moving items from a folder to another but within another subdirectory - powershell

I have the text files in one file (C:/test/ex135). I want to move these files onto a new destination but the ex135 is hidden inside another directory named bob. So I would want to move these text files to C:/test/bob/ex135. Can I have powershell search for "ex135" within certain file path and move items here? If I knew the destination file, I would simply do copy-item -path "C:/test/ex135" -Destination "C/test/bob/ex135" but the destination path is not clear (I would want powershell to find this destination).

Suppose all txt files to be transported are in c:\test\ex135
the target folder is ex135 but in an unknown subdirectory, somewhere deep down c:\test\bob
then
$find_ex135 = Get-ChildItem "C:\test\bob" -Recurse | Where-Object { $_.PSIsContainer -and $_.Name.StartsWith("ex135")}
Get-ChildItem -Path C:\test\*.txt | Move-Item -Destination $find_ex135.FullName

Related

Why doesn't my Powershell loop grab a PDF file if im calling -Include "*.pdf"

foreach ($file in Get-ChildItem -Path $srcRoot -File -Include "*.pdf" -Force -Recurse {
Above is just a line out of my script that is moving files from one directory to another. Long story short.. My script is working and has been for months. However, today I came across where it didnt move two files that were named like the following.
Thisismyfile.pdf2.pdf
Thisisanotherfile.pdf2.pdf
Now like I said.. the script has been working fine until these files came about. Of course I told the users to make sure they name files correctly ect.. but I dont know why it still didnt move those files. It still contains "*.pdf" as an extenstion.. so what gives?
I suspect that files are not moved in scenario where you have file in folder A with the same name as in folder B. Moving files to the one destination folder will cause name collision with error like Move-Item : Cannot create a file when that file already exists.
If that is the case, please use one of snippets from this answer: Powershell Move-Item Rename If File Exists
I placed few .pdf files (named like a.pdf.pdf, b.pdf.pdf ...) in src directory, running snippet below moves those files to dst folder correctly.
$srcRoot = "C:\Users\$env:username\Desktop\src\"
foreach ($file in Get-ChildItem -Path $srcRoot -File -Include "*.pdf" -Force -Recurse)
{
Move-Item -Path $file -Destination "C:\Users\$env:username\Desktop\dst\"
}

Powershell Recursive Copy Except Files found in Exclusion directory

Hi I have a folder Called "A" and folder "A" has files and sub folders within it. I also have another folder directory called "Exclusion" with some copied files and folders from "A" within it. I'm looking for a Powershell script or Command Line option that will COPY & MOVE all the objects from A that are NOT found in the Exclusion directory to a new folder directory called "Output".
Thanks,
-B
Use Get-ChildItem to get a list of files in your exclusion directory, then take only the names of the files and hold those in an array.
Optionally use New-Item with the -Force parameter to ensure that your output directory exists before sending files there.
Next use Get-ChildItem to iterate through all files in our source (A) directory, use Where-Object and the -notin operator to exclude any files which have the same names as those gathered from your exclusion directory, then use Move-Item to move the files to your destination (Output) directory.
[string[]]$filenamesToExclude = Get-ChildItem -Path 'c:\somewhere\exclusion' -Recurse | Select-Object -ExpandProperty Name
New-Item -Path 'c:\somewhere\output\' -ItemType 'Directory' -Force | Out-Null #ensure the target directory exists / don't output this command's return value to the pipeline
Get-ChildItem -Path 'c:\somewhere\A' -Recurse | Where-Object {$_.Name -notin $filenamesToExclude} | Move-Item -Destination 'c:\somewhere\output\'

Powershell: Why is recurse not working?

Hi I have a script that should ideally copy an item and paste it onto a destination folder including all of it's sub folders. However I cannot get it to copy the item into the subfolders.
Here is the code:
Copy-Item "\\postowl\PEC Group\HR\Performance snapshot\Performance Snapshot Macro.xlsm" -Destination "\\postowl\PEC Group\HR\Performance snapshot\2017-2018" -Recurse -Force
Please help!
Recurse copies the subfolders and item from the source to the destination, preserving directory structure. It doesn't recurse in the destination and make multiple copies in the destination. You could use a ForEach-Object loop from the output of Get-ChildItem where you retrieve all of the folders you wish to copy to.
Get-ChildItem "\\postowl\PEC Group\HR\Performance snapshot\2017-2018" -Directory -Recurse | % {
Copy-Item "\\postowl\PEC Group\HR\Performance snapshot\Performance Snapshot Macro.xlsm" -Destination $_.fullname -Force }
Two things from my observation.
1)You are telling you are copying an item including its sub-folders, but in the source you have mentioned one file name with extension. not sure if thats the folder name in your case.
2) I found that in the destination you are not going inside the final level folder.
So put this and check:
Copy-Item "\\postowl\PEC Group\HR\Performance snapshot\*.xlsm" -Destination "\\postowl\PEC Group\HR\Performance snapshot\2017-2018\" -Recurse -Force
Note: If its a specific file, then you can remove the wildcard because that will copy all the files with the extension .xlsm . I have kept it because I am assuming you have only one file under that folder.
See the Documentation and the issue with PS:
Copy-Item Nature & Issue

Powershell Copy files and folders

I have a PS script which Zips up the previous months logs and names the zip file FILENAME-YYYY-MM.zip
This works
What I now want to do is copy these zip files off to a network share but keeping some of the folder structure. I currently a folder structure similar to the following;
C:\Folder1\
C:\Folder1\Folder2\
C:\Folder1\Folder3\
C:\Folder1\Folder4\Folder5\
There are .zip files in every folder below c:\Folder1
What I want is for the script to copy files from c:\folder1 to \\networkshare but keeping the folder structure, so I should have 3 folders and another subfolder in folder4.
Currently I can only get it to copy the whole structure so I get c:\folder1\... in my \\networkshare
I keep running into issues such as the new folder structure doesn't exist, I can't use the -recurse switch within the Get-ChildItem command etc...
The script I have so far is;
#This returns the date and formats it for you set value after AddMonths to set archive date -1 = last month
$LastWriteMonth = (Get-Date).AddMonths(-3).ToString('MM')
#Set destination for Zip Files
$DestinationLoc = "\\networkshare\LogArchive\$env:computername"
#Source files
$SourceFiles = Get-ChildItem C:\Sourcefiles\*.zip -Recurse | where-object {$_.lastwritetime.month -le $LastWriteMonth}
Copy-Item $SourceFiles -Destination $DestinationLoc\ZipFiles\
Remove-Item $SourceFiles
Sometimes, you just can't (easily) use a "pure PowerShell" solution. This is one of those times, and that's OK.
Robocopy will mirror directory structures, including any empty directories, and select your files (likely faster than a filter with get-childitem will). You can copy anything older than 90 days (about 3 months) like this:
robocopy C:\SourceFiles "\\networkshare\LogArchive\$($env:computername)\ZipFiles" /E /IS /MINAGE:90 *.zip
You can specify an actual date with /MINAGE too, if you have to be that precise.
How about Copy-Item "C:\SourceFiles\" -dest $DestinationLoc\ZipFiles -container -recurse? I have tested this and have found that it copies the folder structure intact. If you only need *.zip files, you first get them, then for each you call Resolve-Path with -Relative flag set and then add the resultant path into Destination parameter.
$oldloc=get-location
Set-Location "C:\SourceFiles\" # required for relative
$SourceFiles = Get-ChildItem C:\Sourcefiles\*.zip -Recurse | where-object {$_.lastwritetime.month -le $LastWriteMonth}
$SourceFiles | % {
$p=Resolve-Path $_.fullname -relative
copy-item $_ -destination "$DestinationLoc\ZipFiles\$p"
}
set-location $oldloc # return back

Powershell - copy files to different destination folders based on file names

Dear Powershell Gurus,
I have a few thousands of files in a folder called C:\Downloads\Signs.
The files are named with their dimensions such as 13X20 abcdjd.psf, 8X20 jdscnjfc.psf, 14X24 dje.psf etc.
What I want to do is to move these files to destination folders created within the C:\Downloads\Signs and the folder names are the dimensions of the file names. Example the folder names will be 13X20, 8X20, 14X24 etc and it depends upon as many unique file names with their dimensions.
So, instead of moving them manually looking at how many files are there in the C:\Downloads\Signs folder and then moving them individually, how can we do it in Powershell?
Thanks,
Sanders.
This script will pick up all psf from the root of the C:\Downloads\Signs folder and will move the files to the destination folders (folders will create if they do not exist):
Get-ChildItem C:\Downloads\Signs -Filter *.psf | Where-Object {!$_.PSIsContainer} | Foreach-Object{
$dest = Join-Path $_.DirectoryName $_.BaseName.Split()[0]
if(!(Test-Path -Path $dest -PathType Container))
{
$null = md $dest
}
$_ | Move-Item -Destination $dest -Force
}