Windows Powershell: Cmdlets
PowerShell is the scripting language developed by Microsoft for administering computers. PowerShell was originally available only on Windows, but Microsoft now supports PowerShell on Linux. This chapter will walk you through some basic uses of PowerShell.
Learning Objectives
You should be able to:
- Describe the PowerShell design philosophy
- Launch the PowerShell prompt
- Launch the PowerShell Integrated Script Editor (ISE)
- Run basic cmdlets
Video Walkthrough
Use this video to follow along with the steps in this lab.
PowerShell Design
Historically, Microsoft took a graphical user interface (GUI) approach to system administration. Users and administrators would use a mouse to navigate menus, check boxes, select items from lists, etc. Graphical interfaces are explorable and sometimes you can navigate a new graphical interface with little training.
One downside of graphical interfaces is that they are hard to automate. It can be tedious to click around a graphical interface for the 1,000th time to do the same task. As the number of systems increased, Microsoft realized that it needed a scripting solution to help automate tasks. PowerShell is becoming a requirement for Windows system administrators, but it is useful for regular users to know as well.
Example case: I had to produce a single PDF with my documents for promotion and tenure. I had to print Excel documents to PDFs, print Word documents to PDFs, combine cover sheets, and then put all of those files in order in a single PDF document. Doing this process manually would have been painful. One change to the order of documents, or an update to a single file would cause me to have to start the whole process from scratch. Instead, I created a PowerShell script that would automate this process. If I changed any document, I just had to re-run the PowerShell script and it would create a single PDF ready for submission. Taking 1 hour to write the PowerShell script probably saved 10 hours of tedious work.
Whereas Linux was designed by many independent developers, Microsoft had sole control over PowerShell's design. One of the design goals was to create a scripting language that was consistent. One of the results was the verb-noun syntax for PowerShell commands. Below are a few sample PowerShell commands:
- Get-ChildItem
- Remove-Computer
- Write-Host
- Get-FileHash
- Get-Help
- Revoke-FileShareAccess
The consistent naming scheme can be easier to learn than the mix of program names on Linux (e.g., cat, grep, ls, mkdir).
Some of the commands in PowerShell are called cmdlets (pronounced commandlets), and some are functions. The distinction is not important for people dipping their toes into PowerShell. These exercises will use command and cmdlet interchangeably, though sometimes they might actually be referring to a function. So if it does not matter, why am I writing this? Well, somebody with deep technical experience with a chip on their shoulder might claim I have no idea what I'm talking about because the terminology lacks precision, and I preemptively want to tell that person that I have a reason for describing things the way I do.
Enough reading. Now doing.
PowerShell Console
There are two main ways to run PowerShell code:
- Type it in the console line by line
- Run small programs in the PowerShell Integrated Script Editor (ISE).
We will start with the console.
- Right-click on the start menu to open the (not) super-secret menu options.
- Run Windows PowerShell.

- You could also just search for PowerShell in the start menu, but that would not be very hackerish.
- You should now see the PowerShell prompt.

- You are now ready to start running PowerShell code.
- Run
Get-ChildItemto list files and folders in your current folder.
Get-ChildItem
- Congratulations! You just ran some PowerShell code. Note that you essentially ran a 1-line program.
Notice that the Get-ChildItem output is similar to the Linux
lscommand. - Run the following commands to see if Windows is case-sensitive.
get-childitem
GET-CHILDITEM
get-CHILDitem
- All of the commands should work. Unlike Linux, Windows does not care about the case of your commands, files, or folder names.
- Run the following command to print something to the console.
Write-Host "Hello world"
- The text "Hello world" should appear in the console. Writing to the host is often useful when writing long scripts to give status updates and debug scripts.
- Several commands have aliases to make scripts more compact. For example,
diris an alias forGet-ChildItem. Run the following code.
dir
- You should see the same output as
Get-ChildItembecause theGet-ChildItemcommand was the command that Windows ran. gciis another alias forGet-ChildItem.- PowerShell commands often accept parameters. For example, the
gcicommand can be used to filter. Run the following code to see if there are any text files in the directory.
gci *.txt
- If you do not have any text files in the directory, the list will be empty.
- Run the following code to see if there are any files or folders that start with the letter "d."
gci D*
- You should have a few, including Desktop, Documents, and Downloads.
- Close your PowerShell console.
Getting Help
PowerShell has built-in help. But sometimes your system will not come with all of the help documentation downloaded. Updating the help documentation must be done with administrative rights.
- Right-click the start menu and launch
Windows PowerShell (Admin). It is important to launch the option that has(Admin)in the name. - Windows will launch a User Account Control prompt asking you to verify that you really want to launch PowerShell in administrative mode. Confirm that you do.
- Run
update-helpto start the update process. (Notice that the prompt may showPS C:\WINDOWS\system32instead of the regular user's home directory.)
Update-Help
- You will see progress bars like the following.

- Do not worry if PowerShell tells you that it cannot update help for certain modules.

- Close the PowerShell console. The next steps do not require administrative access, so it is best to open a new console without administrative access. This partly demonstrates the principle of least privilege. To save ourselves from accidentally breaking things, we will only elevate our permissions to administrative permissions when needed.
- Open a new Windows PowerShell console--not in administrative mode.
- Run the following command to read help about the
Get-ChildItemcmdlet.
get-help get-childitem
- The help will display the syntax for using the command, a description, and places to get more information.
- Sometimes it is helpful to look at examples of how the cmdlets can be run. Every PowerShell cmdlet has help documentation that shows examples. Look at the Get-ChildItem examples with the following command.
get-help gci -examples
- Many examples will be listed and described.
- Close the console.
PowerShell ISE
The PowerShell console is great for 1-line scripts. The PowerShell ISE is used for writing multi-line scripts.
- In the start menu, search for "powershell ise" and launch the app.

The ISE interface can seem overwhelming at first. The following screenshot highlights some important features.

- The ISE is split into two sections. The top portion contains the PowerShell code. The bottom portion is a console where the code output will appear when it is run.
- Lines of code are automatically numbered. Line number
- The working directory is shown in the console.
- There are icons to run the current line of code or selected lines of code.
- The script file name appears on that file's tab. Multiple files can be opened at the same time--each in its own tab.
- Add the following code to the file.
gci
write-host "Done listing files"
- Select all of the text in lines 1 and 2.
- Click the "Run Selection" button (the green play button with the document behind it).
- Notice that the script is output in the console.
- Your results should look similar to the following.

- Close the PowerShell ISE. You do not need to save the script file.
Challenge
Write a 1-line script in the PowerShell ISE that searches for all files with .txt extensions in your home folder and all subfolders. You may need to use get-help gci and get-help gci -examples.
Reflection
- Which design approach results in better software--a distributed approach (like Linux) or a centralized approach (like Windows).
- Which method for writing PowerShell code appeals to you more--in the console or in the ISE?
Key Terms
- PowerShell: A task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET framework.
- PowerShell Prompt: The command-line interface where users can enter PowerShell commands and scripts. It is typically represented by
PS>in the console. - PowerShell ISE: Integrated Scripting Environment, a graphical user interface for PowerShell that provides features such as syntax highlighting, debugging, and script editing to facilitate writing and testing PowerShell scripts.
- cmdlet: A lightweight command used in the PowerShell environment. Cmdlets are specialized .NET classes that perform a single function and are used to automate tasks and manage system configurations.