20 Windows PowerShell Commands You Gotta Know
Hey there! Today, I’m gonna take you on a tour of some super useful and easy-peasy Windows PowerShell commands. These commands are gonna help you with all sorts of stuff, like getting some help when you’re stuck or kickstarting a process.
So, what’s Windows PowerShell, you ask? Well, it’s like the new cool kid on the block when it comes to command-line shells. It’s got a bunch of fancy features that make it more powerful and scriptable than the good ol’ Command Prompt. I’ve been using it for three whole years, and let me tell you, it’s a real game-changer, especially if you’re into making things happen automatically with scripts. But here’s the thing: most folks either don’t know about it or just prefer sticking to the old command-line shell.
But fear not, my friend! Today is your lucky day because I’m about to drop some knowledge bombs on you. I’m gonna share with you a bunch of commands that are not only useful but also super straightforward to use. We’re gonna go through some examples too, so you’ll see just how handy these commands can be. Ready? Let’s dive in!
Hey there! Did you know that Windows PowerShell has got your back when it comes to compatibility? It’s designed to make your life easier by supporting a bunch of commands from the good old Command Prompt. And guess what? You get to enjoy all these features in a new, colorful interface!
Contents
- 1 Need some help? Try out Get-Help!
- 2 Discover Windows PowerShell Commands with Get-Command [gcm]
- 3 Let’s Run PowerShell Commands with Invoke-Command
- 4 Invoke-Expression [iex]
- 5 Setting Execution Policy in Windows PowerShell
- 6 Retrieve an item using Get-Item [gi]
- 7 Copying Files and Directories with the Copy-Item Command
- 8 Deleting Items with the Remove-Item Command
- 9 View Variables
- 10 Modify Variables
- 11 Find Running Processes with Get-Process [gps]
- 12 Start Processes with Start-Process [saps]
- 13 Stop-Process [kill]
- 14 Get-Service [gsv]
- 15 Start a Service with Start-Service [sasv]
- 16 Stop Services with Stop-Service [spsv]
- 17 Converting PowerShell Output to HTML
Need some help? Try out Get-Help!
If you’re new to PowerShell, don’t worry if you find yourself in a sticky situation. Get-Help is here to save the day! It’s like your trusty sidekick, giving you all the info you need about cmdlets, commands, functions, scripts, and workflows in PowerShell.
And the best part? It’s super easy to use! Just type in “Get-Help” followed by the name of the command you want to know more about. For example, if you need info on “Get-Process,” just type in “Get-Help Get-Process”.
Discover Windows PowerShell Commands with Get-Command [gcm]
Do you want to explore the powerful commands and features of Windows PowerShell? Well, you’re in luck! With Get-Command, you can easily find the commands you’re looking for.
All you have to do is use the Get-Command followed by your search term in PowerShell. For example, if you’re interested in commands related to services, simply type Get-Command *-service*. This will display a list of commands with “-service” in their name. Don’t forget to use asterisks on both sides of your query because they act as wildcards, helping you search for the unknown.
So go ahead, unleash the full potential of Windows PowerShell with Get-Command! The world of commands and features awaits you.
Let’s Run PowerShell Commands with Invoke-Command
When you want to run a PowerShell command or script, either on your own computer or on multiple computers, you can rely on the power of “Invoke-Command.” It’s a handy tool that makes batch-controlling machines a breeze.
All you need to do is type “Invoke-Command” and follow it with the command or script you want to run, along with its complete path. For instance, you can easily execute a command like “Get-EventLog” by using the “Invoke-Command -ScriptBlock” option. And if you want to run it on a remote computer named “Server01,” you just need to add the “-ComputerName Server01” flag.
Invoke-Expression [iex]
Hey there! I’m here to tell you all about the fabulous Invoke-Expression command, also known as iex. This nifty command is like a magician. It can run other commands or expressions with a flick of the wrist. But here’s the kicker – it only works locally!
So, how does it work? It’s as easy as pie. Just type in “Invoke-Expression” followed by a command or expression of your choice. Let’s say you want to assign the variable “$Command” with the string “Get-Process”. All you have to do is run “Invoke-Expression $Command” and presto! “Get-Process” will magically run as a command on your trusty local computer.
Invoke-WebRequest [iwr]
I can help you with the Invoke-WebRequest command! With this command, you can easily download, log in, and gather information from websites and web services while using Windows PowerShell.
To use this command, simply type Invoke-WebRequest followed by the necessary parameters. For instance, if you want to retrieve the links on a specific webpage, you can use the following command:
Invoke-WebRequest -Uri “https://docs.microsoft.com”.Links.Href
This command will fetch all the links available on the webpage provided. It’s a great way to explore and gather information from different websites.
Give it a try and see how it can enhance your web browsing and data-gathering experience with PowerShell!
Setting Execution Policy in Windows PowerShell
If you’ve ever tried to create or run scripts with a .ps1 extension in Windows PowerShell, you may have encountered some security restrictions. However, there’s a way to adjust the security level – and that’s where the Set-ExecutionPolicy command comes in.
With the Set-ExecutionPolicy command, you can change the security level by choosing one of four options: Restricted, Remote Signed, All Signed, or Unrestricted. For example, if you want to assign the restricted policy status, simply use Set-ExecutionPolicy -ExecutionPolicy Restricted.
Retrieve an item using Get-Item [gi]
If you need to find information about an item in a specific location, like a file on your computer’s hard drive, Get-Item is the perfect tool to use in Windows PowerShell. It’s important to note that Get-Item won’t give you the contents of the item, such as files and subdirectories, unless you specifically ask for them.
To use Get-Item, simply type the command followed by a path or a string, along with any additional parameters. For example, if you want to see all the items (files or folders) that start with the letter “M” in your current directory, you can use the command Get-Item M*. Get-Item can also retrieve the content of registry keys, in addition to directories.
Copying Files and Directories with the Copy-Item Command
If you ever need to duplicate files and directories on your storage disk or copy registry entries and keys, you can do so easily using the Copy-Item command. It works similarly to the “cp” command in the Command Prompt, but it’s even better.
With the Copy-Item command, you can also rename items while copying them. Just provide a new name as the destination. For example, if you want to copy and rename “Services.htm” as “MyServices.txt,” you can use this command: Copy-Item “C:\Services.htm” -Destination “C:\MyData\MyServices.txt”.
Deleting Items with the Remove-Item Command
If you want to remove items like files, folders, functions, registry keys, or variables, the Remove-Item command is perfect for the job. What I find interesting is that it offers parameters to include or exclude specific items.
You can use the Remove-Item command to delete items from specific locations using parameters. For example, if you want to delete the file “MyServices.txt”, you can use the command:
Remove-Item “C:\MyData\MyServices.txt”.
When you need to view the content of a text file at a specific location, you usually have to open and read it in a text editor like Notepad++. However, in Windows PowerShell, you can use Get-Content to retrieve the content without actually opening the file.
For example, if you want to retrieve 50 lines of content from the file “Services.htm”, you can use the command:
Get-Content “C:\Services.htm” -TotalCount 50.
And finally, we have the Set-Content command, which is used to write or replace the content of a file.
You can use the Set-Content command to save text to files, just like the “echo” command in the Bash Shell. With the help of the Get-Content command, you can also grab the content of one file and copy it into another file.
Let’s say you want to update a file with new content. You can simply use Set-Content to write or replace the existing content in the file. Here’s an example: Get-Content “C:\Services.htm” -TotalCount 50 | Set-Content “Sample.txt”. This command will retrieve the first 50 lines from the “C:\Services.htm” file and save them in a new file called “Sample.txt”.
View Variables
When working with variables in Windows PowerShell, the Get-Variable command is your go-to tool for quickly checking their values. It neatly organizes the information in a table, and you have the power to filter and search using wildcards.
To use this command, simply type “Get-Variable” followed by any options or parameters. For example, if you want to see the value of a variable called “desc”, just enter the code “Get-Variable -Name ‘desc'”.
Modify Variables
Need to change or assign a new value to a variable? The Set-Variable command is here to help. For quick assignments, you can use the shortcut format $< $VarName = VarValue >$. For example, to set a variable called “desc” to the value “A Description”, you can use the code “$desc = ‘A Description'”.
You can use the Set-Variable command to assign a value to a variable. For example, by using the Set-Variable command with the Name parameter set to “desc” and the Value parameter set to “A Description”, you can set the value of a variable named “desc”.
Find Running Processes with Get-Process [gps]
When I want to see what processes are currently running on my computer, I usually turn to the trusty Task Manager. But did you know that in Windows PowerShell, there’s a command called Get-Process that can give you that same information, and even allow you to do more with it?
All you have to do is type Get-Process followed by your search query. For example, let’s say you want to find information about processes that have the word “explore” in their name. Just type Get-Process *explore* and hit Enter (don’t forget the asterisks!).
Start Processes with Start-Process [saps]
Starting processes on your computer is a breeze with Windows PowerShell. This is particularly useful if you’re into scripting and automation, as it’s one of those essential commands you’ll definitely need.
With the Start-Process command, you can easily launch one or more processes with a single line of code. Whether you’re opening an application or running a script, this command has got you covered.
If you want to open a program using Windows PowerShell, you can use the Start-Process command. It’s pretty simple – all you need to do is type “Start-Process” followed by the name of the program you want to open. In this case, let’s say you want to open Notepad. You would type “Start-Process -FilePath “notepad””.
But wait, there’s more! You can even add some parameters to the command to make it do more specific things. For example, you can use the “-Verb” parameter to specify how you want the program to be run. If you want to run the program as an administrator, you would type “-Verb runAs” after the program name.
So, to sum it all up, if you want to open Notepad as an administrator using the Start-Process command in Windows PowerShell, you would type “Start-Process -FilePath “notepad” -Verb runAs”. And just like that, Notepad will open up like magic!
Stop-Process [kill]
Hey there! Did you know that you have the power to stop specific or all instances of a process running on your computer? It’s true! And you can do it using a special command called Stop-Process. This command allows you to stop processes by their name or PID (Process ID). But wait, there’s more! With Stop-Process, you can even check if a process is already stopped, and you can stop processes that aren’t owned or started by you.
So how does it work? It’s simple! Just type in the command Stop-Process, followed by the necessary parameters, in order to stop the processes you want. For example, if you want to stop all the processes of Notepad, just type in Stop-Process -Name “notepad”. And voila! Notepad will be closed and all its processes will be stopped.
Get-Service [gsv]
Do you ever wonder about the services running on your computer? Well, wonder no more! When you need information on specific services, whether they’re running or stopped, you can rely on Get-Service to provide you with all the details. Get-Service is a powerful command that shows you all the services installed on your system, and it even gives you options to filter and include or exclude certain services.
If you want to use this command, just type Get-Service and add its parameters. For instance, type Get-Service Where-Object to see the services that are currently running on your computer.
Start a Service with Start-Service [sasv]
If you want to start a service on your computer, you can use the Start-Service command in Windows PowerShell. It’s a powerful tool that can even start a service if it’s disabled on your computer.
To use the Start-Service command, you just need to provide the name of the service. For example, you can start the “Windows Search” service on your local computer by typing Start-Service -Name “WSearch”.
Stop Services with Stop-Service [spsv]
If you want to stop running services on your computer, the Stop-Service command is what you need. Simply specify the name of the service along with Stop-Service. For example, you can type Stop-Service -Name “WSearch” to stop the “Windows Search” service on your computer.
Converting PowerShell Output to HTML
Did you know that PowerShell can give you valuable information about your computer system? The only problem is that it’s not always presented in an easy-to-understand way. But don’t worry, there’s a solution! You can use the ConvertTo-HTML command to create a customized and nicely formatted report that you can analyze or share with others.
All you need to do is combine ConvertTo-HTML with the output of another command. Let me show you how it works. Say you want to see a list of all the services on your computer and their status. You can simply run the following command:
Get-Service | ConvertTo-HTML -Property Name, Status > C:\Services.htm
This command takes the output of the Get-Service command, formats it as an HTML table, and saves it as a web report in a file called “Services.htm” on your C drive. Now you have a neat and organized report that you can open in your web browser or send to someone.
So next time you need to analyze or share PowerShell output, remember to use ConvertTo-HTML. It’s a powerful tool that can transform your information into a user-friendly format.