40 Most Useful PowerShell and Command Prompt Commands for Windows Administrators
By Victor ASHIEDU
Sign up to my PowerShell mailing list.
To receive PowerShell Freebies from Itechguides.com, click this link:
Itechguides.com/powershell-list-amazon/
To get the best of this FREE PowerShell & CMD eBook, use the Table of Contents
(See next page).
Table of Contents
Introduction
This free eBook lists and explains the 40 most useful PowerShell commands and Command Prompt commands. Each command comes with examples.
The book is divided into 2 chapters. Chapter 1 covers the 20 most useful PowerShell commands. Chapter 2 covers the 20 most useful Command Prompt commands.
40 Most Useful PowerShell and Command Prompt Commands for Windows Administrators is for administrators that want to learn the skills to automate Windows tasks with PowerShell or Command Prompt commands.
Chapter 1: 20 Most Useful PowerShell Commands
This guide teaches you how to use the 20 most useful PowerShell commands for Systems Administrators.
In this guide, I will share commands required to perform common tasks in Windows. Most Windows administrators will find this tutorial both useful and handy.
1.0 PowerShell Commands to Find and Get Help with Cmdlets
You cannot talk about the most useful PowerShell commands without learning how to find them. Below are the PowerShell commands that will help you find Cmdlets (Command Lets).
Get-Command
The Get-Command Cmdlet is the first and most important command a PowerShell newbie should learn and know how to use. Why? It helps you find other PowerShell Cmdlets. What command can be more important than a command that can do this?
To find all PS Commands in your computer, simply execute this command below:
Get-Command
Understanding the Results of the Get-Command Cmdlet
There are four columns in the results of the Get-Command Output
- CommandType : This tells you whether a command is an Alias, a Cmdlet, or a Function.
- Name : The name is the actual command you execute.
- Version : This is the PowerShell version
- Source : The module of the PS command.
With this information, you can filter the results from Get-Command. Say you want to see PowerShell commands containing the word "EventLog", running the command below will get the job done:
Get-Command -Name *EventLog
Notice where I added the asterisks. This is because I am aware that
"EventLog" is the "Noun" part of the Cmdlets. However, if you don't even know you could try adding the asterisks at the beginning then try the end.
Below is the result of the previous command.
Get-Command Parameters
Lastly, before we move on, lets discuss the parameters of the Get-Command Cmdlet.
To get all the parameters and information about the Get-Command command, execute this command below:
Get-Help Get-Command -Full
This will give you all the information regarding the Get-Command Cmdlet. I will discuss the Get-Help Cmdlet next.
Get-Help
While the Get-Command Cmdlet finds the Cmdlets, the Get-Help PowerShell command gives you the information you need to use the command.
The easiest way to use the Get-Help Cmdlet is to enter Get-Help followed by the command you want information on. To find more information about the Get-EventLog Cmdlet, run the command below:
Get-Help Get-EventLog
This will give you the basic information about Get-EventLog PowerShell Command. See the result below:
Some Important Parameters of the Get-Help Command
Like any other PowerShell Cmdlet, the Get-Help Cmdlet has several parameters. Below are the most important parameters you will need.
- -Detailed : The Detailed parameter gives you the command SYNTAX, PARAMETERS, ALIASES, and REMARKS.
- -Full : The Full gives similar information provided by the Detailed parameter with more information about each parameter
- -Examples : Gives examples of how to use the Cmdlet. This can be very useful if you have never used the Cmdlet before.
- -Online : Opens the online help page of the Cmdlet.
To see the parameters of a PS Cmdlet, type the Cmdlet in PS, hit the space key, type hyphen "-" followed by the tab key. As you press the tab key you will scroll through available parameters.
1.1 PowerShell Commands to Manage Files and Folders
Now that you know how to find PowerShell commands, let's get you in the hood. The next set of the most useful PowerShell commands are Cmdlets to help you manage files and folders.
Get-ChildItem
Gets items in a specified location. To list the folders in my drive C, I will run the command below:
Get-ChildItem c:/
This will list all the top-level folders. To list all files, folders include sub-folders use the - Recurse parameter.
Tip
You can combine the Get-ChildItem Cmdlet let with other Cmdlet to calculate the size of each folder in a specified directory.
Copy-Item and Move-Item
You could use the Get-ChildItem Cmdlet to list items in a folder, then pipe the result to Copy-Item Cmdlet to copy the items to a new location. The command below will do the job:
Get-ChildItem C:\Dropbox | Copy-Item -Destination C:\NewFolder
The above PowerShell command will only copy the top-level folders and files - it will NOT copy sub-folders and files. To copy all files and folders including sub-folders, include the - Recurse parameter in the Get-ChildItem command as shown below:
Get-ChildItem C:\Dropbox -Recurse | Copy-Item -Destination C:\NewFolder
While the Copy-Item Cmdlet copies items from one location to another the Move-Item Cmdlet moves the item.
Remove-Item
This Cmdlet deletes specified items. Like the Copy-Item and Move-Item Cmdlets, you could pipe the output of Get-ChildItem to Remove-Item.
Use the Remove-Item Cmdlet with caution as it can delete all files and folders in your computer including Windows files!
Tip
By piping the output of Get-ChildItem to Remove-Item, you could create a simple script that will delete some log files on regular bases. You could schedule the PS script to run at a specified time using Windows Scheduler.
New-Item
This Cmdlet creates a new item in Windows. New-Item can be used to create files, folders and registry keys and entries. The command below creates a text file called weekly_file.txt in c:\logfiles folder:
New-Item -Path c:\logfiles -Name weekly_file.txt
Here is the command in PowerShell
Rename-Item
Rename-Item Cmdlet is used to rename things in Windows. This Cmdlet can rename files, folders and registry keys. This command will rename weekly_file.txt to monthly_file.txt
Rename-Item -Path C:\logfiles\weekly_file.txt -NewName monthly_file.txt
When you run the command, it appears that nothing happened, but when you check the folder, the text file has been renamed!