Getting Started with PowerShell
What is PowerShell?
PowerShell is a powerful, cross-platform scripting language developed by Microsoft and has been integrated into Windows since Windows 7. PowerShell can be used interactively (in a terminal) or by saving and running blocks of code (scripts). It has integrations with many Microsoft products, making it easy to get helpful output without much effort. PowerShell is used extensively within the IT Security and System Administration disciplines, but it can benefit almost all IT positions.
Opening PowerShell
If you are running Windows version 7 or newer, then a quick way to access Powershell is by right-clicking the Start menu and then clicking "Windows PowerShell" or "Terminal" (depending on the Windows version). This will open into the command line (shell), perfect for running cmdlets one at a time. When you are more comfortable and want to start writing scripts, you will instead want to open Windows PowerShell ISE (Integrated Scripting Environment). Searching for PowerShell in the Windows search bar will display both options and 32-bit (x86) versions.
Linux and Mac users must first install PowerShell from the PowerShell GitHub page:?https://aka.ms/powershell-release?tag=stable.
Running Cmdlets
PowerShell commands are called cmdlets (pronounced "command-lets"). Cmdlets are usually in the form of "Verb-Noun." For example, you can use the cmdlet?Get-ChildItem?to list all contents of your current directory. Parameters are additional parts of cmdlets that change how they work. For example, we can use the parameters -Path "C:\" and -Hidden to have?Get-ChildItem?show all hidden items in the C:\ directory. The cmdlet would look like?Get-ChildItem?-Path "C:\" -Hidden.?
Discovering What You Can Do
Three cmdlets will help you immensely throughout your time using PowerShell:?Get-Command,?Get-Help, and?Get-Member.
Get-Command?allows you to find new commands that you want to use.?
Working with Comma Separated Value (CSV) files? Use?Get-Command?-Noun CSV to see all the cmdlets that allow you to manipulate them.?
Do you want to start a service but need to know the exact cmdlet? Try?Get-Command?-Verb Start. The Windows PowerShell ISE has a built-in Command Explorer, which is more user-friendly, but I recommend trying?Get-Command. It gives you an excuse to experiment on the command line.
Get-Help?gives you information about a wide variety of PowerShell topics. Before using?Get-Help, open PowerShell as an administrator on your machine and run the cmdlet?Update-Help. Once you do, try running?Get-Help?-Name Get-ChildItem. You will see that we were scratching the surface of what the cmdlet?Get-ChildItem?can do. Even more information (in a friendlier format) is available if you run?Get-Help?-Name?Get-ChildItem?-Online. By adding the -Online parameter, you are taken to Microsoft's website with reference documentation, including examples and detailed descriptions of parameters.
Get-Member?helps you learn what you can do with objects. If you have a thing, "ABC," you could run?Get-Member?-InputObject "ABC." You will see that "ABC" is a System. String and many methods you can use to play with it.?
But wait, what is a Method? How do you use them? Get Help to the Rescue!
领英推荐
If you happened to run?Get-Help?-Name?Get-Help, you'll see in the description, "Conceptual help articles in PowerShell begin with about_." We could run?Get-Help?-Name about_Methods to understand what methods are and how to use them in PowerShell. You will find that 'To perform or "invoke" a method of an object, type a dot (.), the method name, and a set of parentheses "()." If the method has arguments, place the argument values inside the parentheses.' So looking back at the methods for Strings, we could try "ABC." ToLower() to convert the letters to lowercase.
Not Everything Works on the First Try
If you try running?Update-Help?in a PowerShell session that is not running as administrator, you will see?a lot?of red. What happened?
At the time of writing, the first line that appeared for me was "Update-Help: Failed to update Help for the module(s):" and then lists all of the PowerShell modules that were installed (which is quite a few by default). This means that updating the help could have been a lot better.
So what do you do from here? A few lines down, you might see: 'Access is denied […] start Windows PowerShell by using the "Run as Administrator" command, and try rerunning Update-Help.'
When using PowerShell, not all error output will be this helpful. But it will always give you an excellent place to start. Don't be discouraged when things don't work as expected. More than likely, the issue that you are running into is common. Looking up errors in a search engine like DuckDuckGo or Google will almost always yield helpful results quickly.
Where Do I Go From Here?
Everyone learns differently, so here are a few suggestions for free, high-quality resources that you can use to learn PowerShell.
I suggest Mike F. Robbins's book?PowerShell 101?for people who like reading. The book is made available for free on Microsoft's Website here (navigation is on the left):
If you prefer to watch videos, John Savill released a great set of videos on YouTube:
For people who want guided practice, try vexx32's PSKoans GitHub project:
If these resources don't do it for you, Warren F created a complete list of resources here:
Check out the arthur of this article - Glen Banks