Managing files with PowerShell
Use these simple PowerShell commands in Windows to batch-organise files on your computer.
Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell is installed on all Windows machines can be used to manage computer files. The following scripts automate a number of simple functions which would be useful for those with many files to manage.
Add a prefix to files of type in a directory
In PowerShell, change to the target directory (using the cd
command), then enter the following code to add the prefix “AdamDimech” to all .jpg files:
Dir *.jpg | rename-item -newname { "AdamDimech" + $_.Name}
Add a prefix to multiple file types in a directory
Change to the target directory and then enter the following code to add the prefix “AdamDimech” to all .jpg and .png files:
Get-ChildItem -include @("*.png", "*.jpg") | rename-item -newname { "AdamDimech" + $_.Name}
Add a prefix to all files in a directory and its subdirectories
Change to the target directory and then enter the following code:
Get-ChildItem -recurse | rename-item -newname {"AdamDimech " + $_.Name}
Replace a substring in file names
Change to the target directory and then enter the following code to replace the prefix “AdamDimech” with “Adam” in all files, including in subdirectories:
Get-ChildItem -recurse | rename-item -newname { $_.name -replace "AdamDimech","Adam" }
To target files of type, use:
Get-ChildItem -recurse -include @("*.png", "*.jpg") | rename-item -newname { $_.name -replace "AdamDimech","Adam" }
Delete a string from all file names in a directory and subdirectories
Change to the target directory and then enter the following code to remove the string “Adam” from all files, including in subdirectories:
Get-ChildItem -recurse | rename-item -newname { $_.name -replace "Adam","" }
Move files based on a string
Enter the following code and change the $source
and $destination
parameters to select files for relocation. Owing to the -recurse
function, subdirectories will also be searched and files moved from them. This code will create the destination folder if it does not already exist.
$source="C:\path\of\source\files\*e*.*" #include rules for file selection
$destination="C:\path\of\source\files\destination"; #location where files will be moved to
New-Item -ItemType Directory -Force -Path ($destination); Get-ChildItem -recurse ($source) | Move-Item -Destination ($destination)
In this instance, any file with an ‘e’ in its name will be relocated. The position of wildcards can be moved in order
Move files based on file extensions
The code above can be altered if all files of certain extension types should be moved from one directory into another (in this case, all .jpg and .png files):
$source="C:\path\of\source\files" #target directory
$destination="C:\path\of\source\files\destination"; #location where files will be moved to
New-Item -ItemType Directory -Force -Path ($destination); Get-ChildItem -recurse ($source) -include @("*.png", "*.jpg") | Move-Item -Destination ($destination)
Check first!
Clearly, there can be significant consequences if one of these scripts are executed and contain an error. To simulate the command before running it, include a -whatif
function and see what happens.
Comments
One response to “Managing files with PowerShell”
In relation to the last example, “Move files based on file extensions”.
You’ve selected file types through a hashtable and moved them to a destination.
How could you go about having this so that it will produce folders based on the previous hashtable and place the extension types into those specific folders.