PowerShell - Introduction and Examples
Where PowerShell Really Excels
PowerShell is a powerful tool for doing tasks on a computer (or network), that might be time consuming or difficult for a person using other methods.
What needs to be done on a computer may be able to be easily described, but there may be no known available command or program to do the required task.
One solution may be to write software to achieve the task, but that may be overly time consuming or difficult.
PowerShell has many available functions allowing for complex automated tasks to be done according to exact criteria or if-then type logic.
Below we describe two cases in which we found PowerShell useful, and provide the scripts we wrote and used to accomplish those tasks. Feel free to use the scripts yourself.
Searching for Types of Files within Specific Date Ranges
Here is a script that can be used to search a given folder and all its subfolders for two different types of files that were last worked on within a specific date range.
The script outputs the full filename (including the folder path), of all files that are .txt or .docx files and between the dates of 2020 and 2025, showing also the last write time of the file:
ForEach ($file in Get-ChildItem "C:\Users\Name\Desktop\My-Work\" -recurse)
{
if (
($file.LastWriteTime -gt "2020/1/1" -and
$file.LastWriteTime -lt "2025/1/1") -and
($file.extension -eq ".docx" -or
$file.extension -eq ".txt")
)
{
Write-Host $file.fullName " " $file.LastWriteTime
}
}
Using PowerShell for Complex File Tasks
Rename all files according to a specific criteria
We needed to rename many files in a folder according to an
exact but complex criteria.
We had files names of the format:
image(23).png
But we wanted them in this format:
image-23.png
We opened up Windows PowerShell ISE, navigated to the folder with the files, wrote and ran the following script
which renamed all the files according to our requirements:
# This script replaces all filename in the current folder based on a criteria
# Note the user must be in the folder where you want to rename the files
# use cd (the new folder path to get to the current directory before running this script)
ForEach ($file in Get-ChildItem) {
$fileName = $file.Name
$fileFullName = $file.FullName
Write-Host $fileName;
Write-Host $fileFullName;
# search criteria - if the file name contains an open bracket
if ($fileName.Contains("("))
{
# replace the open bracket with a dash
$fileNameReplaced = $fileName.Replace('(', '-')
# remove the closing bracket
$fileNameReplaced = $fileNameReplaced.Replace(')', '')
Write-Host "Replaced Filename"
Write-Host $fileNameReplaced
# rename - you may want to comment out the following line of code
# during testing to check the results will be what you need
Rename-Item -Path $fileName -NewName $fileNameReplaced
Write-Host "Filename Replaced"
}
Write-Host "---"
}
Explanation of the Code
The script basically queries all the files in the folder,
each file is assigned to an object,
the file name properties are acquired,
the filenames are checked if they match the criteria,
if so, a new filename is created according to the needed format,
this is then passed to a rename function which renames the file with the new name.
Summary
This resources page for PowerShell may be expanded in the future.
PowerShell can perform many types of tasks not only related to the file system, but can do many other things like:
- monitor a computer's performance and memory
- query system hardware
- monitor processes
- run SQL queries
- monitor system logs
- request web pages
- and various other things
If you need work done relating to automating complex tasks, or any of the above functions, please contact us.