Working with PowerShell
Windows PowerShell is a powerful tool that you can use to configure a Windows 10 device using the Command-Line Interface (CLI). PowerShell allows desktop support staff and administrators to create reusable scripts to make more complex configuration changes quickly or apply changes to multiple devices.
Windows PowerShell is an integrated shell environment that enables scriptable, flexible, and comprehensive management of Windows 10:
Windows PowerShell has several features that make it ideal for the local and remote management of one or more Windows 10 devices. These features are as follows:
- Windows OS integration: This is a built-in feature in Windows 10 and other earlier Windows versions, such as Windows 7 and Windows Server 2016.
- Remote management capability: You can use PowerShell to manage remote computers.
- Script-based execution: You can use PowerShell scripts to build automation and complex logic into tasks.
You can use PowerShell to run individual cmdlets (pronounced command-lets) that perform actions or to run scripts that use cmdlets. Using PowerShell is much simpler than using other scripting languages, such as Visual Basic Script (VBScript).
PowerShell uses Windows PowerShell drives to provide access to data stores. These drives present data in formats such as a file system. Some common Windows PowerShell drives are as follows:
- The C drive is the local file system's C drive.
- The cert drive is the local certificate store.
- The Env drive contains the environmental variables that are stored in memory.
- The HKCU drive is the HKEY_CURRENT_USER portion of the registry.
- The HKLM drive is the HKEY_LOCAL_MACHINE portion of the registry.
- The Variable drive contains the variables that are stored in memory.
Let's move on to the next section, where we will look at the CLI.
Using the Command-line interface
Commands are PowerShell's main functionality. There are several types of commands, including cmdlets, functions, and workflows. These commands are building blocks, designed for piecing together and implementing complex and customized processes and procedures. PowerShell provides a CLI that you can use to enter cmdlets interactively.
Using the Graphical User Interface
PowerShell is not restricted to the command line. For example, the Active Directory Administrative Center in Windows Server 2012 R2 and Windows Server 2016 is a Graphical User Interface (GUI) that uses Windows PowerShell to perform all of its tasks.
Windows PowerShell ISE
There is another PowerShell app in the same app area called Windows PowerShell Integrated Scripting Environment (ISE), which allows you to see all the available commands and the parameters that you can use with these commands. You also can use a scripting window to construct and save PowerShell scripts.
The ability to view cmdlet parameters ensures that you are aware of the full functionality of each cmdlet and can create the correct PowerShell commands. Windows PowerShell ISE provides color-coded cmdlets to assist you with troubleshooting. Windows PowerShell ISE also provides debugging tools that you can use to debug simple and complex PowerShell scripts. You can use Windows PowerShell ISE to view the available cmdlets by module, as shown in the following screenshot:
Let's look at cmdlets in a bit more detail in the next section.
Understanding cmdlets
Cmdlets use a naming convention of a verb or action followed by a noun or a subject. For example, to retrieve a list of services, you would use the Get-Service cmdlet. This standardization makes it easier to learn how to accomplish administrative tasks. Some common cmdlet verbs are as follows:
- Get: This retrieves data.
- Set: This establishes or modifies data.
- New: This creates a new object.
Each cmdlet has options called parameters. Some parameters are required while others are optional. The parameters vary for each cmdlet. The following example shows how to start the Application Identity service by using the Name parameter:
Start-Service –Name "Application Identity"
Important Note
The cmdlets that are available for use on a computer system vary depending on its Windows PowerShell version and the snap-ins with the cmdlets that are installed.
Let's move on to understanding the compatibility with command-line tools.
Understanding compatibility with command-line tools
You can run batch and executable files in Windows PowerShell. For example, you can run ipconfig.exe in Windows PowerShell and it behaves exactly as if would if you ran it from the Command Prompt. This allows you to start using Windows PowerShell as your default command-line environment for administration. Note that there are also equivalent cmdlets that return similar values as older executables. For example, the cmdlet alternative to ipconfig.exe /all is Get-NetIPAddress, which returns a somewhat similar dataset.
In some cases, commands or options for commands contain reserved words or characters for Windows PowerShell. In these cases, you can enclose the command in single quotation marks to prevent Windows PowerShell from evaluating the reserved word or combination of words. You can also use the grave accent (`) character to prevent the evaluation of a single character.
In some rare cases, an executable file will not run correctly in Windows PowerShell. You should test batch files to ensure that they work properly in Windows PowerShell.
Getting help with using Windows PowerShell
You can use several cmdlets to help you use Windows PowerShell. One of the key cmdlets to get help is the Get-Help cmdlet. Using Get-Help followed by the name of the cmdlet you need help with will give you a brief but detailed guide on that cmdlet, including the parameters that you can use.
For example, we can run the following command:
PS C:\Users\PacktMD100> Get-Help Get-NetIPAddress
It returns the following result:
NAME
Get-NetIPAddress
SYNTAX
Get-NetIPAddress [[-IPAddress] <string[]>] [-InterfaceIndex <uint32[]>] [-InterfaceAlias <string[]>]
[-AddressFamily {IPv4 | IPv6}] [-Type {Unicast | Anycast}] [-PrefixLength <byte[]>] [-PrefixOrigin {Other | Manual
| WellKnown | Dhcp | RouterAdvertisement}] [-SuffixOrigin {Other | Manual | WellKnown | Dhcp | Link | Random}]
[-AddressState {Invalid | Tentative | Duplicate | Deprecated | Preferred}] [-ValidLifetime <timespan[]>]
[-PreferredLifetime <timespan[]>] [-SkipAsSource <bool[]>] [-AssociatedIPInterface
<CimInstance#MSFT_NetIPInterface>] [-PolicyStore <string>] [-IncludeAllCompartments] [-CimSession <CimSession[]>]
[-ThrottleLimit <int>] [-AsJob] [<CommonParameters>]
ALIASES
None
REMARKS
Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
-- To download and install Help files for the module that includes this cmdlet, use Update-Help.
Another useful cmdlet is Get-Command. This cmdlet shows a list of all the cmdlets, aliases, functions, workflows, filters, scripts, and applications installed on your version of Windows PowerShell.