Windows PowerShell is a scripting language and task automation framework for system administration both on-premises and remotely. PS comes preinstalled in every new Windows-based system now.
PS code is generally very short and effective due to the availability of a huge library of cmdlets pertaining to any module.
![](https://bimregime.wordpress.com/wp-content/uploads/2021/06/image-3309110367-e1682402041368.png)
Some people may find this blue screen similar to Command Prompt, but it is much more powerful than traditional CMD. To put it differently, PowerShell can be seen as an enhanced version of Command Prompt with added features and capabilities.
Here are some examples of PowerShell workflows that can automate everyday tasks on Windows.
- Managing files and folders: you can use PowerShell to create, copy, move, and delete files and folders in bulk.
- System configuration: It can be used to automate the configuration of system settings such as network settings, Windows updates, and user accounts.
- Managing processes: It can help you automate the monitoring and management of processes running on your system, such as starting, stopping, and restarting services.
- Software deployment: It can be used to automate the deployment of software to multiple computers, making it easier to roll out new software or updates.
- Task scheduling: It can be used to create and manage scheduled tasks, allowing you to automate routine tasks such as backups, system maintenance, or running reports.
How to Use PowerShell?
There are many flavors of PowerShell, you may even run it on other IDEs (e.g. VS, VS Code) but for simplicity, Windows PowerShell ISE (Integrated Scripting Environment) works as an editor, debugger and provides ready IntelliSense.
If you have a PowerShell script file (*.ps1) and want to run it, you can simply right-click on it and choose to run it with PowerShell. However, it’s advisable to review the script and modify the inputs if necessary before executing the file. To do this, you can open PowerShell ISE, load the PowerShell file, review and edit the code as needed, and then run it by clicking the “Run Script” button (shortcut: F5). This ensures that you have the opportunity to verify and adjust the script before executing it.
![](https://bimregime.wordpress.com/wp-content/uploads/2023/04/image-2.png?w=583)
Let’s see some of the everyday workflows where PowerShell can be a great time-saver.
1. Rename Files
Here we are directly renaming files by adding a prefix to the file name and changing the file name text to the title case.
![](https://bimregime.wordpress.com/wp-content/uploads/2023/04/image.png?w=1024)
# Set the folder path and custom prefix
$folderPath = "C:\Users\{Username}\Documents\_Docs Temp\_Temp"
$prefix = "RAC_"
# Get all files in the folder
$files = Get-ChildItem $folderPath
# Loop through each file and rename it
foreach ($file in $files) {
# Get the new file name with prefix and title case
$newName = $prefix + ((Get-Culture).TextInfo.ToTitleCase($file.BaseName)) + $file.Extension
# Rename the file
Rename-Item $file.FullName -NewName $newName
}
This PowerShell script renames all files in a folder with a custom prefix and title case. It gets all the files in the folder, loops through each file to generate a new name with the prefix and title case base name, and renames the file with the new name.
2. Rename Multiple Files at Once using Excel / CSV
To rename large numbers of files located somewhere in the local drive at once using an external spreadsheet or CSV (Comma-separated Value) file seems to be the easiest workflow to anyone because of the quick and familiar way of editing text, finding and replacing text or using formulae in Excel.
![](https://bimregime.wordpress.com/wp-content/uploads/2023/04/image-5.png?w=571)
![](https://bimregime.wordpress.com/wp-content/uploads/2023/04/image-3.png?w=1024)
# Set the folder path and import the CSV file
$folderPath = "C:\Users\{Username}\Documents\_Docs Temp\_Temp"
$csvPath = "C:\Users\{Username}\Documents\_Docs Temp\RenameFilesWithCSV.csv"
$fileNames = Import-Csv $csvPath
# Loop through each file name in the CSV file and rename the file
foreach ($fileName in $fileNames) {
$existingName = $fileName.ExistingNames
$newName = $fileName.NewNames
$filePath = Join-Path $folderPath $existingName
if (Test-Path $filePath) {
Rename-Item $filePath -NewName $newName
Write-Host "File '$existingName' renamed to '$newName'"
}
else {
Write-Host "File '$existingName' not found in folder"
}
}
This script renames files in a folder using a CSV file that contains “ExistingNames” and “NewNames” columns. The script imports the CSV file and loops through each row to extract the existing file name and new file name. It then checks if the file exists in the folder and renames it using the Rename-Item cmdlet if it does. Finally, it outputs a message indicating whether the file was renamed or not.
3. Create Multiple Folders
With PowerShell, you have the capability to create multiple folders simultaneously by importing a CSV or Excel file. These folders can have any number of subfolder levels as desired.
![](https://bimregime.wordpress.com/wp-content/uploads/2023/04/image-1.png?w=1024)
This script creates multiple folders using a CSV file. It starts by setting the current location where the folders are to be created. Then, it imports a CSV file containing a list of folder names to be created.
Using a foreach loop, the script creates each folder in the CSV file by using the New-Item cmdlet with the type parameter set to the directory. The folder names are specified by the column header “FolderNames” in the CSV file.
# Location where your folders are to be created
Set-Location "C:\Users\{Username}\Documents\_Docs Temp\_Temp\Multiple Folder Creation\Folders Created"
# Import CSV file from location
$Folders = Import-Csv "C:\Users\{Username}\Documents\_Docs Temp\_Temp\Multiple Folder Creation\FoldersToBeCreated.csv"
# Create Folders from CSV ensure your have a heading of FolderNames in your CSV file
ForEach ($Folder in $Folders) {
New-Item $Folder.FolderNames -type directory
}
To sum up, PowerShell is a powerful tool that can automate a wide range of tasks and processes in your daily work routine. Whether you need to manage files and folders, configure system settings, or deploy applications, PowerShell can help you save time and streamline your workflow.