Unleashing the Full Potential of the PowerShell.Utility Module

As for me this is the most important and critical module for your PowerShellFu.

This module is part of the built modules package that is provided to you out of the box, with the installation of PowerShell.

As it is stated in documentation: This module contains cmdlets that manage the basic features of PowerShell.

Well, based on my experience, this magnificent collection of cmdlets easily covers 80% of your PoweShell activities.

It contains 100+ stunning cmdlets, number of cmdlets vary depending on the PowerShell version and OS type. At the time of writing, the PowerShell Desktop on Windows version contains 107, PowerShell Core on Windows – 115 and PowerShell Core on Linux – 111.

Usage Use Case(s)

Let me provide you my mind map and use cases that PowerShell.Utility module can be applied to.

I’ve grouped all cmdlets in 10 main Use Cases.

Improve Productivity

Based on the number of cmdlets in this category, main focus of this module is to provide user with the basic (as mentioned in documentation) tools to be successful and unleash the full PowerShell potential.

34 (~30% ) of the cmdlets in PowerShell.Utility module are designed to jump start your PowerShell adoption journey. We can categorize them even further into more specific 8 subcategories.

1. Aliases management

Essentially, aliases are the shortcuts or alternate names for cmdlets, functions, and commands. Aliases are used to make PowerShell cmdlets shorter and easier to remember.

In Powershell.Utility module there are 6 cmdlets for aliases management: Get-Alias, New-Alias, Set-Alias, Remove-Alias, Import-Alias, Export-Alias.

Use the Get-Alias cmdlet to retrieve all the aliases that are available in the current session, including built-in aliases, as well as any custom aliases that you have set, imported, or added to your PowerShell profile.

To create a new alias, you can use the New-Alias cmdlet. Aliases created using New-Alias are not saved after you exit the session or close PowerShell.

Leverage Set-Alias to create or change aliases for specific functions, scripts, files, or other executables. If you need to remove an alias from the current session, you can use the Remove-Alias cmdlet.

And finally, if you need to import or export an alias list from or to a file, you can use the Import-Alias and Export-Alias cmdlets, respectively.

2. Culture Settings

Culture settings in PowerShell refer to the regional and language-specific settings that are used by the PowerShell console and scripts. By default, PowerShell uses the culture settings that are defined by operating system.

There are 3 cmdlets used for culture settings: Get-Culture, Get-UICulture, Import-LocalizedData.

Use the Get-Culture cmdlet to retrieve information about the current language settings on the system, such as the keyboard layout and the display format for numbers, currency, and dates.

In addition, you can use the Get-UICulture cmdlet to retrieve information about the current UI culture settings in the operating system. The UI culture determines which text strings are used for user interface elements, such as menus and messages.
If you need to import language-specific data into your scripts and functions based on the UI culture selected for the operating system, you can use the Import-LocalizedData cmdlet. It’s designed to enable scripts to display user messages in the UI language selected by the current user.

3. Events Management

Events are a way to trigger an action or script in response to a specific occurrence or condition. Typical usage is to automate tasks, monitor system activity, and perform other types of actions.

There are 8 cmdlets available out of the box: Get-Event, Get-EventSubscriber, Register-EngineEvent, Register-ObjectEvent, New-Event, Remove-Event, Unregister-Event, Wait-Event.

Use the Get-Event cmdlet to retrieve events (which you have registered or created) that are in the event queue. To retrieve event subscribers in the current session, you can use the Get-EventSubscriber cmdlet.

Leverage Register-EngineEvent cmdlet to subscribe to events generated by the PowerShell engine and the New-Event cmdlet. Helps you to forward the events, specify an action to respond to the events, or cancel the subscription.

Execute the Register-ObjectEvent cmdlet to subscribe to events generated by a Microsoft .NET Framework object on the local or remote computer. Once the subscribed event is raised, it is added to the event queue.

To create a new event, you can use the New-Event cmdlet. Custom events can be used to notify users about state changes in your program or any change that your program can detect, including hardware or system conditions, application status, disk status, network status, or the completion of a background job.

To delete events from the event queue, you can use the Remove-Event cmdlet. To cancel an event subscription, you can use the Unregister-Event cmdlet. This deletes the event subscriber from the session and prevents subscribed events from being added to the event queue.

Finally, you can use the Wait-Event cmdlet to suspend execution of a script or function until a particular event is raised.

4. System

This category contains 2 cmdlets: Get-Host, Get-Uptime. They are used to retrieve information about the current host program and amount of time that the computer has been running since the last reboot. This can be useful if you need to check the current operating system, architecture or how long a server or workstation has been running without interruption.

5. Variable Management

Variables are used to store and manipulate data in scripts and commands. They can hold a wide range of data types, including strings, integers, arrays, and more.

Powershell.Utility module contains 5 cmdlets for variables management: Get-Variable, New-Variable, Set-Variable, Clear-Variable, Remove-Variable.

To retrieve variables, use the Get-Variable cmdlet. To create a new variable – use the New-Variable cmdlet. Use it to assign a value to the variable during creation, or you can assign or change the value after it is created. The Set-Variable cmdlet can be used to set the value of a variable. If a variable with the requested name does not exist, the cmdlet creates the variable.

To delete the value of a variable – use the Clear-Variable cmdlet. This deletes the data stored in the variable, but it does not delete the variable itself. To completely delete a variable and its value, leverage the Remove-Variable cmdlet. Except constants or variables owned by the system.

6. Learning

This category helps to learn more about PowerShell and cmdlets available.

There are 2 cmdlets available: Get-Verb, Show-Command. These ones really endures your command discovery and learning experience.

The Get-Verb cmdlet can be used to retrieve a list of approved PowerShell verbs that can be used when naming your own commands.

Show-Command is particularly useful for teaching and learning, as it presents a command window that lists all available commands in all installed modules.

7. Scripts Development

This category present scripts that can be leveraged for your custom functions and scripts.

It contains 6 cmdlets: Import-PSSession, Invoke-Expression, Measure-Command, Send-MailMessage, Start-Sleep, Write-Progress.

The Import-PSSession cmdlet can be used to import commands from another session, including those from customized shells or other modules and snap-ins. Use Invoke-Expression to evaluate or run a specified string as a command and return the results.
For measuring execution time of script blocks and cmdlets, the Measure-Command cmdlet is your friend.
The Send-MailMessage cmdlet can be used to send an email message.

The Start-Sleep cmdlet can be used to suspend activity in a script or session for a specified period of time. And finally, the Write-Progress cmdlet can be used to display a progress bar within a PowerShell command window, with the ability to select indicators for the bar and the text above and below it.

8. File Actions

This one is pretty self explanatory, it contains 2 cmdlets: New-TemporaryFile, Unblock-File.

Create temporary files using the New-TemporaryFile cmdlet. This creates an empty file with a .tmp file extension and a random hexadecimal number in the file name. The file is created in the TEMP folder.

The Unblock-File can unblock files that were downloaded from the internet. Use it when you want to run PowerShell script files that were downloaded from the internet, as they are typically blocked by default. By using Unblock-File, you can bypass this block and run the file even when the PowerShell execution policy is set to RemoteSigned.

PowerShell Debugging

This category consists of powerful set of debugging features that can help with troubleshooting and diagnosing issues in PowerShell scripts and functions. There are 16 cmdlets: Debug-Runspace, Disable-PSBreakpoint, Disable-RunspaceDebug, Enable-PSBreakpoint, Enable-RunspaceDebug, Get-PSBreakpoint, Get-PSCallStack, Get-Runspace, Get-RunspaceDebug, Get-TraceSource, Remove-PSBreakpoint, Set-PSBreakpoint, Set-TraceSource, Trace-Command, Wait-Debugger, Write-Debug.

Start an interactive debugging session within a runspace, set breakpoints on a line, command, or variable, and display the current call stack using Get-PSCallStack.
Disable and enable breakpoints using Disable-PSBreakpoint and Enable-PSBreakpoint respectively. (Pretty obvious and self-explanatory).
Get-PSBreakpoint can be used to get a list of all active breakpoints in the current session, and Remove-PSBreakpoint can be used to delete breakpoints from the console.
To trace PowerShell components, use Trace-Command. Get-PSTraceSource can be used to get trace sources for PowerShell components that are currently in use. Write-Debug can be used to write a debug message to the console, which can be displayed by using the Debug parameter or the $DebugPreference variable.
To manage debugging on runspaces, use Disable-RunspaceDebug and Enable-RunspaceDebug. These commands disable or enable debugging on one or more runspaces and release any pending debugger stops.
Finally, Stop-Debugger stops a script in the debugger before running the next statement in the script.

PowerShell Troubleshooting

Cmdlets in this category involves identifying and resolving issues with PowerShell scripts and commands.

Main goal is to understand what is going on during the script or function execution and meticulously read the error messages.

It contains 6 cmdlets: Get-Error, Write-Error, Write-Host, Write-Information, Write-Verbose, Write-Warning.

The Get-Error command retrieves the most recent error messages from the current session, while the Write-Error command writes an object to the error stream, declaring a non-terminating error. The Write-Host command writes customized output to a host, which is typically used to produce display-only output such as printing colored text. The Out-Default command specifies how PowerShell handles information stream data for a command.
On the other hand, the Write-Verbose command writes text to the verbose message stream, which is used to deliver more in-depth information about command processing, while the Write-Warning command writes a warning message.

TimeStamps

This category groups cmdlets for dates manipulations. There are 3 cmdlets: Get-Date, New-TimeSpan, Set-Date.

The Get-Date cmdlet retrieves the current date and time. The New-TimeSpan cmdlet creates a TimeSpan object to represent a time interval. And the Set-Date cmdlet allows you to change the system time on your computer to a specific time and date of your choice.

API Interaction

There is only one cmdlet in the category that can be used to interact with APIs – Invoke-RestMethod. It’s a versatile cmdlet that can be used to retrieve data from and send data to a RESTful API, send data to a RESTful API, or modify existing data in a RESTful API. Because this one cmdlet is so multifaceted, it deserves a separate post to dive deep into the functionality and use cases.

Data Format

This category includes 8 cmdlets that are used to display data in different ways: Export-FormatData, Format-Custom, Format-Hex, Format-List, Format-Table, Format-Wide, Get-FormatData, Update-FormatData.

The Export-FormatData cmdlet allows to save formatting data from the current session in an XML file. The Format-Custom cmdlet uses a customized view to format the output, providing flexibility in displaying views that are not just tables or lists. Format-Hex displays the input as hexadecimal, helping to determine the file type of a corrupted or unknown file.
The Format-List cmdlet formats the output as a list of properties in which each property appears on a new line, while Format-Table formats the output as a table with the selected properties of the object in each column. The Format-Wide cmdlet formats objects as a wide table that displays only one property of each object.
To manage the formatting data, PowerShell provides Get-FormatData and Update-FormatData cmdlets. The former retrieves the formatting data in the current session. The latter reloads the formatting data from formatting files into the current session, allowing you to update the formatting data without restarting PowerShell.

Data Manipulations

This is just a summary of the Data Manipulations capabilities; I have written the whole series of posts related to this topic. Please refer to How to use PowerShell for Data Manipulations for more information.

Data Retrieval

Groups built-in cmdlets that help to retrieve data from a variety of sources such as databases, files, and web services. Leverage PowerShell to execute SQL queries, parse log files, or scrape data from web pages.

There are 5 cmdlets: Import-Clixml, Import-Csv, Import-PowerShellDataFile, Invoke-WebRequest, Read-Host.

Please refer to Empower your Data Retrieval with PowerShell for more details.

Data Processing

Set of cmdlets to perform various data processing tasks such as filtering, sorting, and grouping. Leverage PowerShell to manipulate data in a flexible and efficient way.

It contains 11 cmdlets: Compare-Object, Get-FileHash, Get-Random, Get-Unique, Group-Object, Measure-Object, New-Guid, Set-MarkdownOption, Show-Markdown, Sort-Object, Test-Json.

Please refer to Make your Data Processing more efficient with PowerShell for more details.

Data Transformation

Please refer to Data Transformation Made Easy with PowerShell for more details.

Data Export

Please refer to Data Export with PowerShell: Best Practices and Advanced Techniques for more details.

Summary

To sum it up, the awesome PowerShell.Utility module contains quite a few cmdlets.

  • ~40% of the cmdlets are related to different manipulations with data like data retrieval, processing, transformation and export;
  • ~30% of the cmdlets can be leveraged to improve your productivity and help you to master PowerShell faster;
  • ~14% of the cmdlets are dedicated to debugging activities of your PowerShell scripts and functions;
  • ~16% are other cmdlets that cover large range of use cases: PowerShell Troubleshooting, Date & timestamps manipulation and API Interaction.

To my subjective opinion this the one of most useful built in PowerShell modules.

Thanks a lot for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.