Automating Everyday Tasks With PowerShell: Bulk Folder Creation & Renaming

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.

PowerShell comes preinstalled with Windows

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.


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.

Renaming files
# 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.

Sample CSV file
Renaming Files Using the CSV file
# 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.

Bulk folder creation

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.

Visualizing Collatz conjecture with Python

Collatz conjecture aka 3n+1 problem is notoriously famous for being the simplest to explain and yet unsolved problem in mathematics.

The problem goes like this: take any positive integer number; if it’s even – double the number, if it’s odd –triple it and add 1. Keep doing this to the new number you get until you finally reach 1. The conjecture is that after all these iterations, you always reach 1, no matter which positive integer you started from.

While no one has proved the conjecture, it has been verified for every number less than 268, which is more than 295 quintillion or 295 billion billion.

Here we are going to simulate this problem using Python. I’m using VS Code but you can prefer to use any IDE.

import matplotlib.pyplot as plt

num = int(input("Enter a positive integer: "))
seqList = [num]

while (num > 1):
    if num % 2 == 0:
        num = num/2
    else:
        num = 3 * num + 1
    seqList.append(num)   

print("Number of steps: ", len(seqList)-1)
print("Max step value: ", max(seqList))
print(seqList)

# Plotting graph
x = list(range(0,len(seqList)))

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_ylim(0,max(seqList)+5)

# x and y axis
plt.plot(x, seqList)

# Annotating graph
for i,j in zip(x, seqList):
    ax.annotate(str(j), xy=(i+0.1,j+0.5))

plt.xlabel("Number of steps")
plt.ylabel("Value")
plt.grid()
plt.show()
Number 27 unexpectedly hops up and down 111 times reaching a maximum higher than the elevation of Mt. Everest in meters. Interestingly, 27 is the only positive integer that is 3 times the sum of its digits.
Plot for number 15

3 Innovative AI Powered UI/UX Design Tools

It is well known fact that better design translates to better value. Here are some AI driven tools that may make your work easy.

1. Sketch2Code

Sketch2Code is an AI technology built by Microsoft AI Labs that can convert your handwritten sketch of a User Interface and convert it to HTML code.

This is like a translation tool for your design to make a working HTML snippets. Sketch2Code is part of Azure Cognitive Services where OCR is used to comprehend your handwritten text and the Computer Vision APIs are used for object detection.

Sketch2Code

Just grab your rough design on a paper or napkin and upload it on Sketch2Code. The project is yet in beta stage so it may not produce best results and as with all Machine Learning tools it requires enough inputs to achieve greater accuracy in its pattern predictability.

2. Attention Insight

Attention Insight is an AI driven analytics tool to check your designs before launching them to make better judgements based upon the heatmaps generated with predictive eye-tracking technology.

It helps you to implement A/B testing (split testing) with two or more options where you can visually see how smartly each part of the design captures the customer’s attention and identifies the areas of interests i.e. logo, graphics, layout, text etc.

Posters with attention hotspots

3. Khroma

Khroma uses AI to make a truly never-ending combination of appealing color palettes based on your initial set of color selection. Just choose 50 of the shades that you find attractive and let it do the rest of the calculation to produce a limitless amount of color palettes, types, posters, gradients, and images. Use them in your next product — website, video, poster, or application.