October 2024 PowerShell Potluck
Here we are once again at the end of another month. I hope the content this month has been helpful and informative. If you are not a premium subscriber, I hope you'll consider trying at least a monthly subscription for a few months. You can cancel at anytime and in the meantime you get full access to the archive of content. Even though this is categorized as a newsletter, I approach each "issue" more of a short book chapter or online articles. These monthly wrap-ups are the most newsletter-ish that I get.
PowerShell Summit 2025 CFP
2025 will be here in no time and that means another edition of the PowerShell+DevOps Global Summit. While I hope you'll consider attending, I'm even more hopeful that you'll consider proposing a session presentation. I think everyone has something they can teach others. Maybe you solved a problem at work with PowerShell. Or perhaps you've learned something about PowerShell that isn't widely known. You don't have to be an expert. Sessions typically are 45 minutes and if you include demos, you don't need to prepare an extensive PowerPoint presentation. When I present, I try to keep my PowerPoint use to the bare minimum.
And if you don't feel you have much experience presenting, this is an incredibly safe and welcoming event. Attendees are eager to hear and learn. They want presenters to succeed. And audience sizes won't be overwhelming. The PowerShell community needs the next generation of speakers. Why can't it be you? Submit your sessions at https://sessionize.com/pshsummit25 by November 15, 2024.
PSEdit
I am a big fan of terminal user interfaces (TUIs). Especially because I spend so much time at a PowerShell prompt. One such TUI you might want to try is called PSEdit
. This is a PowerShell module written by Adam Driscoll of PowerShell Universal fame. Adam has written a complete text editor that runs inside a PowerShell prompt. This is perfect for quick and simple edits where you don't need a full-blown editing environment like VS Code.
Install the module from the PowerShell Gallery.
Install-Module PSEdit
It is supported in Windows PowerShell and PowerShell 7. The module has a single command, Show-PSEditor
which has an alias of psedit
.
psedit $profile
In addition to syntax highlighting, you also get command and parameter completion.
If you know the line number, you can open the file directly to that line.
psedit $profile -line 33
You can even debug and run code right from within the terminal editor. You can use keyboard shortcuts or your mouse to access the menus. By default, the editor will also format your PowerShell scripts on closing.
The editor won't work in a remoting or ssh session, although it isn't that difficult to copy files over a remoting session so you could edit locally and then copy the file back.
I wouldn't use this as a replacement for VS Code because there's no much more it has to offer. But the PSEdit module is more than adequate for "quick and dirty" edits and certainly offers more functionality than Notepad.
You can learn more by visiting the project's GitHub repository. My one word of caution is to be careful using this in the integrated VS Code terminal. You might encounter keyboard shortcut conflicts and psedit
is an alias for the VS Code command Open-EditorFile
.
Terminal Icons
While we are on the topic of living in a PowerShell session, another project you might want to add is the Terminal-Icons
module. Install from the PowerShell Gallery.
Install-PSResource terminal-icons -Repository PSGallery -TrustRepository
#OR
Install-Module terminal-icons
The module has plenty of commands:
PS C:\> Get-Command -Module Terminal-icons | Select Name
Name
----
Add-TerminalIconsColorTheme
Add-TerminalIconsIconTheme
Format-TerminalIcons
Get-TerminalIconsColorTheme
Get-TerminalIconsGlyphs
Get-TerminalIconsIconTheme
Get-TerminalIconsTheme
Invoke-TerminalIconsThemeMigration
Remove-TerminalIconsTheme
Set-TerminalIconsIcon
Set-TerminalIconsTheme
Show-TerminalIconsTheme
But you probably won't need to use them much. Instead, you'll need to install an additional font or two, after which, the module will add a little pizzazz to your shell experience.
Before we get to that, you'll need to install one of the Nerd Fonts. I found at least one that I could install using winget.
winget install --id DEVCOM.JetBrainsMonoNerdFont
You will find it easier to use the interactive installer.
& ([scriptblock]::Create((iwr 'https://to.loredo.me/Install-NerdFont.ps1')))
I installed some of the Cascadia Code fonts since that is what I use in Windows Terminal. Once installed, you need to configure your PowerShell windows to use the Nerd font. In Windows Terminal, open settings and go the Appearance part of your profile. Select the Nerd font.
When you import the Terminal-Icons
module, your file and directly listing experience vastly improves.
The module commands are designed to help you view and customize the settings. Run Show-TerminalIconsTheme
to see the icons associated with different file extensions. If you are going to spend a lot of time working from a PowerShell prompt, why not make it enjoyable?
PowerShell Remoting Clean Up
Hopefully, you have been installing PowerShell 7 releases. If you've been installing and configuring remoting, you most likely have a collection of PowerShell remoting configurations.
Get-PSSessionConfiguration |
Where-Object name -match "powershell.7" |
Sort-Object Name -Descending |
Select-Object -skip 1
I'm skipping the first one, because it should be the most current.
These configurations aren't necessarily doing any harm and as far as I know they all provide the same level of functionality. But I like keeping things clean and orderly. If I don't need them, let's get rid of them.
Get-PSSessionConfiguration |
Where-Object name -match "powershell.7" |
Sort Name -Descending |
Select-Object -skip 1 |
Unregister-PSSessionConfiguration -WhatIf
Restart-Service WinRM -Force
I'm using -WhatIf
so you can see what would happen.
You should restart the WinRM service for the changes to take effect. If you accidentally remove all the PowerShell 7 remoting configurations, you should be able to run Enable-PSRemoting
to set things back up.
PowerShell Exit
Speaking of housekeeping, my last item may be of interest if you have a need to do something every time your PowerShell session ends. Because PowerShell is based on .NET, it has an event that fires when PowerShell is exiting. Be careful, though. If you close the terminal window or if PowerShell crashes, the event probably won't fire. The only way you can guarantee the event is to type exit
when you are finished.
Here's an example of how you can use it. You can put this in your PowerShell profile script.
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action {
# Get the history
$history = Get-History
# Convert the history to JSON
$json = $history | ConvertTo-Json -Depth 1
# Save the JSON to a file
$json | Out-File -FilePath "C:\temp\History.json"
}
The Action
script block is the code that will execute when the Exiting
event fires. You should probably keep this action simple. In my example, I'm exporting command history to a JSON file. Nothing earthshaking. You might want to clean temp folders, log your session activity, or send an email to your boss! If you find a use for this event, I'd love to hear about it.
Scripting Challenge
At the end of last month, I gave you a scripting challenge. I thought I would go over a solution here, but I think I will save the solution for my next email. This gives you a little more time to work on it.
In the meantime, here's another scripting challenge for you:
Write a PowerShell function to will show you all Start Menu links. The output should be a custom object with the following properties:
- the link base name
- the link name
- The link's full name with path
- The name of the parent directory
- When the link was created
- The computer name
Bonus: Include a property that shows the target path of the link and a description.
Show-Off Bonus: Create a custom formatting file.
I'll even give you a starting point, C:\ProgramData\Microsoft\Windows\Start Menu\
.
The learning is in the doing so I hope you'll give this a try. I'll share a solution later next month.
Summary
That wraps up this month. Keep using PowerShell every day and don't be afraid to try something new. If there is a topic you'd like me to cover, you are invited to contact me at behind@jdhitsolutions.com.