Learning PowerShell can feel a little like cheating Windows. For decades, Unix and open-source users have stared at command prompts. Now, however, we find ourselves outside the GUI trying to figure our first steps.
PowerShell, unlike other command prompt interfaces creates a scripting environment that is as close as possible to human language. PowerShell is the ideal place to start if you are looking to learn how to administrate from the command line.
These tips will help novice scripters get past common stumbling blocks and veteran scripting pros transition to another environment.
How do I save and write my first script?
You can quickly create and save scripts using a simple notepad. You may already have the PowerShell ISE (integrated Scripting Environment) installed on your machine. The ISE allows you write, debug, save and run your scripts all from one interface.
Learn how to become a security expert with SPOTO’s Cybersecurity Training
Start trainingClick on the start button to start typing PowerShell. Windows PowerShellISE should appear in your list. You can try it by creating a PowerShell version Hello World.
12write “hello world”
No matter what development environment you use files should be saved with the extension.ps1 in order to identify them as PowerShell scripts
How do I execute a program?
Your first PowerShell script has been created and saved! Now, you want to see it in use. This should be easy, right? It is actually quite simple. Simply click the green “Run Script” arrow at the top of the PowerShell ISE.
Eventually, you will want to run scripts from outside of the ISE. This is also very easy. Right-click the script in Windows Explorer and select “Run With PowerShell”.
You can add a prompt to keep the PowerShell window open if it closes abruptly. Add this line to the end your script:
12Read-Host – Prompt “Press Enter for Exit”
How can I quickly open a PowerShell within my current directory?
It opens in your home user directory when you type PowerShell at Cortana search or Windows start button. It will then ask you to navigate to your scripts. This is an alternative. Type “PowerShell” in Windows Explorer and hit Enter. It’s pretty neat, huh?
There is plenty of information available to make PowerShell more accessible for newbies. PowerShell can be made easier by learning shortcuts.
How can I find information about the system environment, such as the name and model of my computer?
PowerShell really shines when it comes to managing complex multi-server and multi-location environments. Properly designed scripts must verify information such as the machine and user account they’re running on during network-wide deployments. PowerShell uses the $env expression in PowerShell to read and modify environment variables. Take this example:
There are many more secure ways to accomplish these tasks but $env is a great starting point because of its simplicity.
How can I simplify the commandlet names of frequently used commands or rename them to correspond with another scripting language?
Some PowerShell commandlets can be a bit confusing or not intuitive. In other environments, “get-command” is simply known as “which”. This can be easily corrected with New-Alias.
12New-Alias that get-command