PowerShell Community
A place for the community to learn PowerShell and share insights
Latest posts
Encrypting secrets locally
Keeping security folks happy (or less upset which is the best we can hope for)
Simple form development using PowerShell
Create .NET forms without all the .NET.
Creating a scalable, customised running environment
This post shows how to create an easy to support environment with all your own cmdlets.
Using PowerShell and Twilio API for Efficient Communication in Contact Tracing
Learn to integrate PowerShell with Twilio API and streamline communication for COVID-19 contact tracing initiatives.
Automate Text Summarization with OpenAI and PowerShell
This easy-to-follow guide shows you how to use PowerShell to summarize text using OpenAI's GPT-3.5 API.
Changing your console window title
This post shows how to change the title of your console terminal window.
Measuring average download time
This post shows how to measure average download time with PowerShell
Measuring script execution time
This post shows how to measure script execution time in PowerShell
Porting System.Web.Security.Membership.GeneratePassword() to PowerShell
This post shows how to port a C# method into PowerShell
Designing PowerShell For End Users
This posts explains taking user experience into account when designing PowerShell tools
Convert specific table of excel sheet to JSON using PowerShell
This posts explains how to Convert Specific Table of Excel Sheet to JSON
Update XML files using PowerShell
This posts explains how to update XML files using PowerShell
Mastering the (steppable) pipeline
The PowerShell pipeline explained from the beginning to the end.
PowerShell Registry Monitor
How to set up a simple registry key monitor with PowerShell
The many flavours of WMI management
Working with the different ways of managing WMI.
Password Expiry Notification Using Teams and Graph API
This post's intent is to show how to use Graph API through PowerShell to send a Teams message.
Cheat Sheet – Console Experience
How to have more control of preferences in functions and the role of modules on inheritance.
Reading Configuration Manager Status Messages With PowerShell
This post's intent is to show how to read Configuration Manager status messages using WMI and Win32 API function FormatMessage.
On Preferences and Scopes
Progress in PowerShell: a tale of Verbosity and other preferences with lessons in Scopes and Proxies thrown in It started, as these things often do, with someone complaining. In PowerShell Version 7.2 the output of and look like this: In all the earlier versions they look like the version below, which is more helpful when you're trying to debug code that builds URIs: A proxy function will fix that. If two commands have the same name an alias beats a function, which beats a cmdlet, which beats an external program. You can specify the full path to an external program or cmdlet - for example so an functio...
How to Preview PowerShell Scripts In PowerShell
Q: When I use Windows Explorer and select a PowerShell script file - I do not see the script in the preview window. Can I fix that? A: You can make a few simple registry updates and do just what you want! At some time in the deep and distant past, Windows Explorer gained the preview pane feature. The idea is simple: you select a file in Explorer and Windows shows you a preview of the file in a separate pane. I love this feature, although when clicking on a Word document, it could take a few moments before I could view. And if you are viewing a file, it was "open" and you could not delete it in a separate window...
How to Use $PSDefaultParameterValues
Q: When I use cmdlets like and , how do I change default values of the Keep and Wrap parameters? A: Use the automatic variable. When I first discovered PowerShell's background jobs feature, I would use to view job output - only to discover it's no longer there. And almost too often to count, I pipe objects to cmdlet only to get truncated output because I forgot to use . I'm sure you all have parameters whose default value you would gladly change - at least for your environment. I'm sure you have seen this (and know how to use Wrap), like this: So, the question is: how to tell PowerShell to always use w...
How to Use $FormatEnumerationLimit
Q: When I format an object where a property contains more than 4 objects, I never see the extra property values. How can I fix that? A: Use the variable. This query is one I hear in many PowerShell support forums, and I have encountered this issue a lot over the years. What happens is that you issue a command to return objects, for example . The cmdlets return objects which can contain properties that are arrays of values, not just a single value. When you pipe those objects to , by default, PowerShell only shows you the first four. Let me illustrate what this looks like (by default): This output shows Po...
How to use the Secret modules
Q: I have a bunch of scripts we use in production that make use of Windows credentials. In some cases, these scripts have an actual password in plain text, while others read the password from an XML file. Is there a better way? A: Scripts with high-privilege account passwords in plain text is not a good idea. There are several methods you can use to improve the security of credentials handling. One great way is to use the SecretManagement and SecretStore modules from the PowerShell Gallery. What are Secrets? Secrets are, in general, passwords you need to access some resource. It might be the password for a dom...
A closer look at the Crescendo configuration
In my previous post, I looked at the details of a Crescendo output handler from my VssAdmin module. In this post, I explain the details of a cmdlet definition in the Crescendo JSON configuration file. The purpose of the configuration The structure for the interface of a cmdlet is a reasonably predictable thing. The pattern of the script code to support these fits a template. The more difficult part of the cmdlet is in the code that does the work. Crescendo separates the functional code (the output handler) from the cmdlet interface code. The Crescendo configuration file defines the interfaces of cmdlets...
A closer look at the parsing code of a Crescendo output handler
In my previous post, I showed you how to parse the output from the command. The output of is not very complex. The goal of the post was to introduce some parsing strategies that you can use to create a full Crescendo module. In this post, I explain the details of a more complex parsing function that I created for my VssAdmin module. Examining the parser for The following screenshot shows the function that is called by the cmdlet to handle the output from . The parsing process is broken into four areas: Collecting the native command output The output from is passed into the output handler functi...
Converting string output to objects
In my previous post, I talked about using Crescendo to create a PowerShell module for the command in Windows. As I explained, you have to write Output Handler code that parses the output of the command you are using. But if you never written a parser like this, where do you start? In this post I show you how to parse the output from the command. The output of is not very complex and it is basically the same on Windows and Linux systems. The goal here is to talk about parsing strategies that you can use to create a full Crescendo module. Step 1 - Capture the output To create a parser you have to capture the ...
Understanding Get-ACL and AD Drive Output
Understanding Active Directory ACL using PowerShell can be a bit tricky. There are no out-of-the-box cmdlets with ActiveDirectory PowerShell module to help in settings the permission quickly. While there are no cmdlets, you can nevertheless manage AD permissions using the AD PowerShell drive. In this post, I will try to simplify Active Directory ACL and how to read the result easily, so let's start. Prerequisites To follow along with this article, you need the following: The domain name used for this tutorial is Contoso.com. Reading Active Directory Permission using Get-ACL Reading Active Directory pe...
My Crescendo journey
In a recent PowerShell Users Group meeting I was thinking that it might be good to talk about the new Crescendo module and how to use it. I was going to ask Jason Helmick if he would do a presentation for us. Then, in an unrelated conversation, someone mentioned using for some project. This got me thinking: is a perfect candidate for a Crescendo module and maybe I should just learn it and do the presentation myself. What is Crescendo? Crescendo is an experimental module developed by Jim Truher, one of the main developers of PowerShell. Crescendo provides a framework to rapidly develop PowerShell cmdlets that ...
How to Make Use Of PowerShell Profile Files
Q: I would like to personalize the way that PowerShell works. I have heard that I can use a thing called a profile to do this, but when I try to find information about profiles, I come up blank. There is no cmdlet, so I do not see how to create such a thing. Can you help me, please? A: Profiles are a powerful part of PowerShell and allow you to customize PowerShell for your environment. They are easy to create and support a range of deployment scenarios. What is a Profile? Before explaining the profile, let's first examine the PowerShell host. A PowerShell host is a program that hosts PowerShell to allow you ...
How can I be notified any time a service goes down?
Q: How can I be notified any time a service goes down? A: The short quick answer to utilizing WMI and PowerShell 7. You use PowerShell to create temporary event monitoring using WMI. Then WMI monitors any service changes and generates an alert once it detects a change. Basic Requirement To achieve this, you need Windows PowerShell 5.1 and above. This post uses the latest version of PowerShell 7. So if you are don't yet have PowerShell 7, see the Microsoft documentation on how to Install PowerShell 7 on Windows. Also, make sure that PowerShell is running as administrator. Finding the Required Class Before ...
How to Update or Add a Registry Key Value with PowerShell
Q: I am having a problem trying to update the registry. I am using the New-ItemProperty cmdlet, but it fails if the registry key does not exist. I added the –Force parameter, but it still does not create the registry key. The error message says that it cannot find the path because it does not exist. Is there something I am not doing? I include my script so you can see what is going on. Help me, please? A: Let's look at how you can use PowerShell to add or update any registry key value. The Registry Before answering the query, let me cover some of the background basics. You probably already know this but I star...
How to Change the Start Page for the Edge Browser
Q: How can I change the Edge startup page? A: You can change the start page easily using PowerShell. Edge and It's Start Page I am basing this article on the latest incarnation of the Edge browser, aka Edge Chromium. The settings in this article seem to work fine on the latest versions of Windows 10 and Server 2022. Other browsers can have different approaches to startup page. And as ever - E&OE! (Errors and Omissions Excepted) When the Edge browser starts up, it displays a startup page based on Bing by default. For many, this is fine - they can browse the contents and then navigate where they want. But i...
How to rename a NIC
Q: Is there a simple way to rename a NIC, especially inside a Hyper-V VM? A: You can change the name of any Windows NIC using PowerShell - whether the NIC is in a physical host or a Hyper-V VM. NICS and NIC names One thing that can quickly become confusing when using Hyper-V with multiple VMs and VM Switches is how fast the network adapters seem to proliferate. You start with a few wired Ethernet Adapters on the host. Then you install Hyper-V and create a VM farm with loads of virtual NICs. Before you know it, you have a dozen adapters inside the VM host and an unclear set of adapters in the VM. To discover t...
How to send output to a file
Q: Is there an easy way to save my script output to a text file rather than displaying it on screen? A: Of course - there are multiple ways to do just that! PowerShell and Output One of PowerShell's great features is the way it automatically formats output. You type a command - PowerShell gives you the output it thinks you want. If the default output is not what you need, use the formatting cmdlets like and to get what you want. But sometimes, what you want is getting output to a file, not to the console. You might want to run a command or script that outputs information to a file and sends this file via ema...
How Do I Discover Changes to an AD Group’s Membership
Q: Is there an easy way to detect and changes to important the membership of AD Groups? A: Easy using PowerShell 7, WMI, and the CIM Cmdlets. WMI Windows Management Instrumentation (WMI) is an important component of the Windows operating system. WMI is an infrastructure of both management data and management operations on Windows-based computers. You can use PowerShell to retrieve information about your host, such as the BIOS Serial number. Additionally, you can perform management actions, such as creating an SMB share. WMI is, in many cases, just another way to do things. For example, you can use WMI to crea...
Sending data to the Clipboard from PowerShell
Q: Hey I have a fun question! I remember reading a while back about using VBScript to paste to the clipboard. Are we able to do that with PowerShell? A: Why yes, yes we can! It is far often a much quicker solution if we start with PowerShell! Pasting content to the clipboard, the old VBScript method Before we show the quick and easy solution, let's learn how we could adapt an older solution. Now back in the day if I wanted to paste something on the clipboard I would go down to the store, get some glue and... "DOH! Wrong Clipboard!" (Knew I should have splashed some water on my face before typing this up!) W...
Borrowing a built-in PowerShell command to create a temporary folder
Q: Hey I have a question for you. It seems silly and I know I could probably put something together with Get-Random. But can you think of another way to create a temporary folder with a random name in PowerShell? Ideally, I'd like it to be in a user's own "Temporary Folder" is possible. A: We sure can! If Doctor Scripto was sitting here right now, I'd see that little green haired person shout out "Never fear, Scripto is here!" New-TemporaryFile Cmdlet Within PowerShell there is a built in Cmdlet called . Running this cmdlet simply creates a random 0 byte file in the in whichever platform you are working in. ...
Is a User a Local Administrator?
Q: Some of the things we do in our logon scripts require the user to be a local administrator. How can the script tell if the user is a local administrator or not, using PowerShell 7. A: Easy using PowerShell 7 and the LocalAccounts module Local Users and Groups The simple answer is of course, easily. And since you ask, with PowerShell 7! But let's begin lets begin by reviewing local users and groups in Windows. Every Windows system, except for Domain Controllers, maintains a set of local accounts - local users and local groups. Domain controllers use the AD and do not really have local accounts as such. You ...
Testing the connection to computers in the Active Directory
Q: As an administrator, I often have to do a lot of reporting on the servers in my domain. Is there a simple way to test the connection to every server in my domain or every server or client host in a specific OU? A: Of course you can do this with PowerShell! You can use the Active Directory cmdlets and , although it is not as simple as one might like. Using the module Microsoft has developed several modules to help you deploy and manage AD in your organisation or via Azure. The module is one which Microsoft ships with Windows Server (although not installed by default). You can also load the Remote Server Ad...
Can I Enable the Caps Lock Key?
Q: I have a script where users enter some information. This information needs to be entered in all capital letters, so my instructions say, “Please make sure the Caps Lock key is on before entering the information.” They don’t always do that, however. Is there a way to turn the Caps Lock key on and off using a script? A: I don't know how to run the key off and on, but with PowerShell, there is a way to mimic the effect of turning on the Caps Lock key. User Input Considered Harmful Let's start with the observation that all user input is harmful. One of my earliest IT heroes, Edsger Dijkstra, published a seminal...
A Reusable File System Event Watcher for PowerShell
FSWatcherEngineEvent module provides events of file system changes as powershell engine event
Determine if a folder exists
Q: Is there any way to determine whether or not a specific folder exists on a computer? A: There are loads of ways you can do this. The Test-Path Cmdlet The easiest way to do this is to use the cmdlet. It looks for a given path and returns if it exists, otherwise it returns . You could evaluate the result of the like in the code snippet below This is similar to the operator for IF statements in Bash. is returned if exists, otherwise is returned. For More Information And for more information on see the Test-Path help page. Summary So as you saw, tests the existence of a path and returns a boolean...
Reading a text file bottom up
Q: I have a log file in which new data is appended to the end of the file. That means the most recent entries are at the end of the file. I’d like to be able to read the file starting with the last line and then ending with the first line, but I can’t figure out how to do that. A: There are loads of ways you can do this. A simple way is to use the power of array handling in PowerShell. The Get-Content Cmdlet Before getting into the solution, let's look at the cmdlet. The cmdlet always reads a file from start to finish. You can always get the very last line of the file like this: This is similar to the co...
Lightning Fast and Easy Provisioning of Git with SSH Key Authentication on Windows
Maybe you have a team of Windows developers that are onboarding for your new Git server installation or maybe you've decided to drop http password authentication to your existing Git server (due to it's many problems). Your next steps may well be into a rough and rocky rabbit hole when you were holding out hope for simplicity (you know the kind you've fallen into before if you've been in tech for more than about 45 minutes). The common Internet guidance for setting up Git with SSH authentication on Windows are unnecessarily complex. My inner tool smith really loathes when the very first steps into something new...
Leveraging XML with New Employee PowerShell Scripts
using XML referencing to bolster up your new employee scripts with information.