You know about Command Prompt. But what is Windows PowerShell?
Windows PowerShell is a task automation tool developed by Microsoft to help administrators reduce their workload. It helps you automate simple tasks and processes in Windows, helping you focus on work involving more complexity and creativity.
While many people have seen and used the Command Prompt, fewer have heard of PowerShell. In this article, we'll look at how exactly PowerShell helps its users, as well as some of the cool things that you can do with it.
What Is Windows PowerShell?
Before we dive into PowerShell, let's first nail down another question: "What’s a shell?"
A shell is a computer program that takes commands through the keyboard, interprets them, and relays them onto the operating system to execute. Essentially, it acts as an interface between you and the operating system. It can either be GUI (graphical user interface) based, or it can be built on top of a CLI (command-line interface).
Although shell has been around since the Multics operating system launched in 1969, Windows introduced its version, the Shell, in November 1985. It was a basic shell that was useful for managing files, called MS-DOS Executive. Developments of this basic shell followed, but there was always a need for something more powerful.
Earlier in Windows history, users resorted to using different tools for system administration and automation. In 2006, a major improvement of the Shell concept was introduced as a solution: Windows PowerShell.
The Windows PowerShell lets you perform various operations, ranging from automating repetitive tasks, network administration, and so on, to tasks that demand more attention and creativity (like website content publishing, designing, etc.).
Not only does this save you time, but it also gives you greater control over the operating system and its processes.
Cmdlets (pronounced command-let) are light and potent Windows PowerShell commands. They exist as building blocks you can use to execute specific actions such as copying and moving files or as part of a bigger, more advanced PowerShell script.
The
Get-Help cmdlet, for instance, is the most useful cmdlet as it lets you see what a particular cmdlet does, look at its parameters, and show the various ways in which the cmdlet is used.
Similar to the
Get-Help,
Get-Command is another cmdlet that you can use to troubleshoot various issues from the shell. Specifically, it shows all the commands installed on your system. The commands displayed include cmdlets, functions, aliases, filters, scripts, and applications. When used with parameters, it will show you all specific commands associated with that parameter.
For instance, the following command will grab commands of all types available to your local computer and displays them:
Get-Command *
The Get-Command with the parameter
ListImported, on the other hand, will only get the commands from current sessions.
Get-Command -ListImported
How to Launch the Windows PowerShellTo start Windows PowerShell, open the Start menu search bar, type
windows powershell, and run select
Run as administrator.
This will launch the PowerShell program on your computer. Alternatively, you can launch it through the Run dialog. Press
Windows key + R on your keyboard, type
powershell, and hit Enter to launch PowerShell.
How to Use Windows PowerShellAs explained above, PowerShell is an application that helps you automate repetitive tasks and manage simple Windows administration tasks, and hence, helps you to save time. Below, we define some of the most useful ways that you can use PowerShell to your advantage.
1. Creating Scripts Using Windows PowerShellA script is a set of instructions, a short program of sorts, that a bigger program runs during its execution.
With PowerShell, you can easily create a script and use it again in the future if the need arises. There are a variety of ways to create a script, but for now, we'll focus on the simplest method: creating a PowerShell script with Notepad.
To create a PowerShell script, follow the steps below:
1. Type
notepad in the Start menu search bar and select the Best Match.
2. In Notepad, write or paste the script you want to use. For example:
Write-Host "I make memes; Therefore I am."
Now, click on the
File option and select
Save As. Enter a relevant name and click
Save.
To run the script, run the PowerShell as an administrator. Then, type the command from below and hit
Enter.
Set-ExecutionPolicy RemoteSigned
This will change the execution policy on your machine and let you run scripts on PowerShell.
Please note that this is a permanent change and that the execution policy restriction is a security feature that can stop you from running malicious scripts. If you're copying and pasting PowerShell scripts from the internet without fully understanding what the script does, this is a useful feature.
Finally, type
A and press
Enter, and type this command to run the script, swapping out
mshaa for your username.
& "C:\Users\mshaa\Desktop
cript.txt"
If you followed the commands in order, your PowerShell script will run without any hiccups.
Remember, you have to run PowerShell as an administrator. Otherwise, you won't be able to change the default execution policy on your system that stops random scripts from running on your system.
An exception will be called instead, and like all exceptions, it will stop the execution of your program midway. Your interface will look something like this:
2. Delete the Content of a Specific FileHave you ever been in a situation where you had to delete all the old content from a file, but you wanted to keep the file itself intact? If so, you can use the clear-content command from PowerShell to get it done.
Clear-Content C:\Temp\TestFile.txt
Swap out the file path for the file you want to clear.
3. Run a Command in a Remote ComputerYou can also use Windows PowerShell to execute a command on a single or multiple remote computers. This functionality of PowerShell is known as PowerShell Remoting. All you need is a single computer at your end and a reliable internet connection.
However, before you execute the command, you'll first have to establish a remote connection between multiple computers. You can use the PSSession command to run remote commands.
Enter-PSSession -ComputerName RemotePCName -Credential UserID
After you connect to the computer, you can execute PowerShell commands as if you would on a local system.
If you want to execute a command on multiple remote computers, run this command:
Invoke-Command -ComputerName Server01, Server02 -FilePath c:\Scripts\DiskCollect.ps1
This snippet will execute the script DiskCollect.ps1 on remote computers, Server01 and Server02. For a detailed introduction to remote commands, check out
Microsoft's official remote command guide.
4. Use PowerShell for a Malware ScanYou can run a malware scan on your system right through Windows PowerShell. To run a quick scan, type the following commands in PowerShell and hit
Enter:
Start-MpScan -ScanType QuickScan
But sometimes, a quick scan is not enough. Thankfully, you can also execute a full scan from PowerShell.
Start-MpScan -ScanType FullScan
Note that the full scan might take some time, so it would be wise to run it in the background. For that, use this command instead:
Start-MpScan -ScanType FullScan -AsJob
This will run a full malware scan in the background while you handle other tasks. For more information, check out our complete guide to
using PowerShell to scan your computer for malware.
5. Tinkering With Files and FoldersYou can even manage your system's files and folders right from a single place with the help of Windows PowerShell. There's a lot of different stuff that you can do, such as moving, opening, renaming, etc. As with the previous commands, switch out
mshaa for your username.
Renaming Files and FoldersYou can easily rename your files and folders using the Rename-Item cmdlet in PowerShell.
Rename-Item c:\Users\mshaa\Desktop\MemesAreLame.xls MemesAreCool.xls
Moving Files and FoldersJust like renaming, you can also use PowerShell to move your computer's files and folders.
Move-Item c:\Users\mshaa\Desktop\MemesAreLame.xls c:\Users\mshaa\Documents
Opening Files Use the command below to open any random file on your system:
Invoke-Item c:\MakeUseOf\*.txt
Replacing the name of a specific file with the asterisk (*), will let the Invoke-Item cmdlet open multiple files at a single go.
Make Windows PowerShell Your AllyHopefully, this short guide helps you get started with PowerShell. As a free application from Microsoft, it's a very useful tool to keep in your bag of Windows tools to make your Windows operation smoother and automated.
source