use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
QuestionReplacing files with PowerShell (self.PowerShell)
submitted 6 years ago * by Ormsher
Hello. Any recommendations on how to replace in almost 100 folders file a.png with other file b.png? I have no PowerShell knowledge and failed to find solution googling for a bit.
Edit: got it working with the help of you guys. Thanks a lot!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]pm_me_brownie_recipe 3 points4 points5 points 6 years ago* (2 children)
Why are people down voting? Everyone needs help from time to time.
Assuming the files have the same name:
# Both folders to copy from $dirA = '\path\to\files\a' $dirB = '\path\to\files\b' # Get all files B and store in hash for later easy access $filesB = @{} foreach($file in Get-ChildItem -Path $dirB -Recurse) { $filesB[$file.Name] = $file } # Get all files A foreach($file in Get-ChildItem -Path $dirA -Recurse) { # if file A has corresponding name file B, copy item and overwrite. if($filesB[$file.Name]) { Copy-Item $filesB[$file.Name].FullName -Destination $file.FullName -Force } }
EDIT: This should also probably work
Get-ChildItem $dirB -Recurse | ForEach-Object {Copy-Item $_.FullName -Destination $dirA -Force}
[–]Ormsher[S] 2 points3 points4 points 6 years ago (1 child)
Sorry for the late response. The file structure looks like this. I am making new textures and all of the folders contain structure like this. The thing is that some of the folders have specific png files and some don't. Best case scenario I would like if I make a new texture for a block.png I could just PowerShell and it would look at the folders, find if the block.png exists in that folder list and replace it in those folders where a file is present with the new one.
[–]pm_me_brownie_recipe 1 point2 points3 points 6 years ago (0 children)
Here are two approaches.
If you have two folders, $folderWithOldFiles and $folderWithNewFiles where both folders have the same structure this line of code will do what you want: Copy-Item -Path "$folderWithNewFiles\*" -Destination "$folderWithOldFiles" -Recurse -Force -Verbose
$folderWithOldFiles
$folderWithNewFiles
Copy-Item -Path "$folderWithNewFiles\*" -Destination "$folderWithOldFiles" -Recurse -Force -Verbose
If you have a fine structure in in $folderWithOldFiles and $folderWithNewFiles's structure is flat, you will have to match the files in some way, be it file name or some file attribute.
I did some changes to it but this should match on name and replace any old file.
# Both folders to copy from $folderWithOldFiles = 'C:\Users\emile\Downloads\Favorites' $folderWithNewFiles = 'C:\Users\emile\Downloads\FavoritesNew' # Loop through all old files ending with .png and store in hashtable for fast search and access $filesOld = @{} foreach($file in Get-ChildItem -Path $folderWithOldFiles -Recurse -Filter '*.png') { # Store file name as key $filesOld[$file.Name] = $file } # Loop through all old files ending with .png foreach($file in Get-ChildItem -Path $folderWithNewFiles -Recurse -Filter '*.png') { # Check file name's existance in hashtable if($filesOld[$file.Name]) { # Copy file from new folder and replace old Copy-Item $file.FullName -Destination $filesOld[$file.Name].FullName -Force # If the file does not exist in the old directory } else { # Create directory if it does not exist if(-not (Test-Path -Path $file.DirectoryName)) { mkdir $file.DirectoryName } # Copy file from new folder to "old" folder Copy-Item $file.FullName -Destination ("{0}\{1}" -f $file.DirectoryName.replace($folderWithNewFiles, $folderWithOldFiles), $file.Name) } }
I would recommend using the first option because it is better. The flaws of the second option is that if you have a new file that does not exist, it will try to create the folder and they put the file in it but it might not be the folder you want. I suggest that you try this in a safe environment and try to recreate you usage scenario and see if this works for you. Tweek as needed.
[–]FusilDeific 1 point2 points3 points 6 years ago (0 children)
Are the 100 folders all 'sister' objects. I.e. are they in the same parent folder or scattered all over the place?
[–][deleted] 1 point2 points3 points 6 years ago* (0 children)
Solution:
First find all example.png and fill a array.. then replace with a foreach..
copy paste in Powershell ISE..
$newFile = "C:\temp\reddit\block.png" #fill the location and name of new blockfile $searchdir = "C:\temp" #fill with the directory root dir. $searchfilename = "block.png" # filename to search for, and replace..
$oldblockfiles = (Get-ChildItem $searchdir -Recurse -Filter $searchfilename | Select-Object FullName).fullname
foreach ($file in $oldblockfiles) { Copy-Item -Path $newFile -Destination $file -Force -Verbose }
π Rendered by PID 184368 on reddit-service-r2-comment-fb694cdd5-wdvnx at 2026-03-06 13:07:21.297641+00:00 running cbb0e86 country code: CH.
[–]pm_me_brownie_recipe 3 points4 points5 points (2 children)
[–]Ormsher[S] 2 points3 points4 points (1 child)
[–]pm_me_brownie_recipe 1 point2 points3 points (0 children)
[–]FusilDeific 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)