Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save milesgratz/4f8da4d6f7fd1a6319b9fb0095121795 to your computer and use it in GitHub Desktop.
Save milesgratz/4f8da4d6f7fd1a6319b9fb0095121795 to your computer and use it in GitHub Desktop.
Find recursive text files and move to parent folder
# Define parent folder
$Parent = "C:\temp\Parent"
# Search for text files recursively
$Files = Get-ChildItem -Path $Parent -Filter *.txt -Recurse
# Loop through files, moving to Parent folder
foreach ($File in $Files)
{
Try
{
# Try moving file
Move-Item $File.FullName -Destination $Parent -ErrorAction Stop
}
Catch
{
# Duplicate filename found (throw error)
$_
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment