Introduction to PowerShell Scripting
All of us are tired of various laborious and monotonous work that is part of our IT ecosystem. They are time-consuming and boring too. But most of these tasks can be automated using various applications and several processes can be automated programmatically. But, not everything requires a complex application or programming to automate the process.
For example, there is a master folder and multiple subfolders inside it. Each subfolder is used to hold files of a distinct format. At the start of the day, the files in the master folder have to be segregated to different folders.
This can certainly be automated. But creating a brand new application for this task might turn out more time consuming than the problem itself. So that leaves us with the question - How can we automate this? The answer is scripts and one of the ways to build a script using PowerShell. In this post, we are going to understand what is PowerShell and also look into the practical implementation of a PowerShell script.
Table of contents
- What is PowerShell?
- Features of PowerShell
- What is PowerShell ISE?
- Practical implementation of PowerShell script
- Wrapping up
What is PowerShell?
Windows PowerShell is an object-oriented task automation framework and scripting language from Microsoft. It is part of the .Net framework and has the C# code forming its foundation.
It combines the flexibility of scripting along with the speed of the command line. Its commands are called cmdlet. Typically, a cmdlet has one or more defined actions and it returns a .Net object.
Basic cmdlets (such as navigating through the folder structure, moving, copying files, etc.) come built-in with PowerShell.
Features of PowerShell
Below are a few of the key features of Windows PowerShell -
PowerShell supports the error handling mechanism by providing ‘try’, ‘catch’, and ‘finally’ statements.
Once the script is created, users can set breakpoints at the required lines and can be used to debug the code to figure out runtime values and issues.
PowerShell supports the execution of scripts on a remote machine.
PowerShell provides options to run cmdlets and scripts in the background asynchronously with a manual trigger. Also, PowerShell provides options to run the scripts on remote machines without interacting with the console in the background.
Constraint run spaces
PowerShell provides options to run the scripts with a set of constraints that provides the ability to restrict access and execution of cmdlets, scripts.
PowerShell provides a transaction feature, which adds extra safety while executing a long-running script. In case of an error, while executing the script, the transaction enables the option to rollback the changes and in case of success, the transaction enables the option to commit the changes.
PowerShell provides options to invoke browsers through code and execute the necessary actions on the website.
Ability to manage system settings
PowerShell is built-in with the power to manage data pertaining to the current system settings, make changes to objects, manage services, and manage file systems.
What is PowerShell ISE?
Windows PowerShell ISE (Integrated Scripting Environment) is a graphical user interface that enables users to create, run, test, and debug PowerShell scripts. PowerShell ISE is an optional feature that comes in with windows.
PowerShell ISE provides -
- Multiline editing - allows users to add lines below the selected lines.
- Selective execution - allows users to run/test the selected portion of the script. Users are required to highlight the selected portion of the code and press the F5 button.
- Syntax coloring - differentiates the keywords and codes that are in the script with different colors. This makes users identify the inbuilt keywords, variables, and user-defined functions easily.
- Tab completion - allows users to use tab to auto-complete the in-built commands and variable names.
- Error indication - saves user’s time by highlighting the syntax errors with a red squiggly line.
Advantages of using PowerShell ISE
- Reduces errors in script creation and saves time.
- Improved debugging and testing capabilities.
- Easy to use and provides insights on related scripts.
Practical implementation of PowerShell script
At the beginning of our blog, we saw a scenario we had to segregate files based on file formats. In this section, let's see how to achieve it using a PowerShell script.
Open the PowerShell ISE by typing the below command from the Run window.
After opening the PowerShell ISE, the first thing to validate is the execution policy. By running the below command, users will be able to see the execution policy that is currently set.
There are 4 execution policies.
- Restricted - This is the default setting and in this setting, scripts cannot be executed.
- AllSigned - Users can run the scripts that are signed by a trusted developer.
- RemoteSigned - Users can run their own scripts or can run scripts that are signed by a trusted developer.
- Unrestricted - Users can run any script they want.
In our case, we are going to update the RemoteSigned execution policy for the current user. To do that run the below command.
Set-ExecutionPolicy -Scope CurrentUser
Once it prompts to type the execution policy, type the below command, and press Enter.
For our case, we are going to create three different files in three different formats and inside three different folders with file format as folder names.
Create a PowerShell script that will get the list of files in the provided directory, and from there it will process each file and move the file to the appropriate sub-folder inside the main directory. Below is a sample PowerShell script for accomplishing that task. This can be fine-tuned to create folders automatically if they are not present.
Once the script is ready, save the script in the desired location and execute the script. In case of any error in the script, it will be reported in the execution space.
Users can validate whether the files are moved to the appropriate folders by using command prompt\PowerShell.
PowerShell includes numerous features that help in automating monotonous tasks. Learning and having solid knowledge in PowerShell enables users to do many administrative functions easily, without the need for third-party software.