PowerShell Potluck - March 2024
Month-end recap: PowerShell presentations, Git aliases, Test-Path tips, future plans. Email behind@jdhitsolutions.com for questions.
Welcome to the end of another month. The first quarter of 2024 is almost in the books. It is a little scary sometimes seeing how fast the days fly by and watching the work pile up on my plate. Fortunately, PowerShell helps a bit, but it can't do my taxes!
I've decided to rebrand this end of the month post. When I started this newsletter I envisioned answering a lot of reader questions, which you are still welcome to do. You can always email me at behind@jdhitsolutions.com or leave a comment on the article in the archive. I have been using this last article of the month as a catch-all for things that don't fit anywhere else. So, I'm going to embrace the idea with a slight name change.
Recent Presentations
I have had a few public speaking engagements in the first quarter. The session material and video recordings are now available.
PowerShell Podcast
Mike F. Robbins and were guests on the PowerShell Podcast chatting about PowerShell and how to build a career around it. Mike is helping me with the OnRamp program at this year's PowerShell Summit. You can download or listen to the episode at https://powershellpodcast.podbean.com/e/crafting-a-fulfilling-career-wisdom-from-industry-leaders-jeff-hicks-and-mike-f-robbins/. If aren't a subscriber, you should be! Or you can watch the interview on YouTube
PowerShell with Style
I also did a presentation for the PowerShell User Group Rhein Neckar from Germany. I talked about how to use PSStyle in your daily work and scripting. Session material and demos are in a GitHub repository. You can watch a recording of the presentation at https://www.youtube.com/watch?v=6LySy7rrUO8
Git Aliases
I spend most of my day working at a command prompt. Even though I'm a decent touch typist, I don't like to type any more than I have to. I take advantage of PowerShell aliases constantly to save keystrokes. They add up over the course of day. Plus, I'm less likely to mistype gsv than Get-Service.
Another place where I uses aliases is with git. For example, instead of trying to remember that git config -l
will list my git configuration, I have an alias so I can do this:
PS C:\> git list
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/etc/ssl/certs/ca-bundle.crt
core.autocrlf=true
...
To see defined aliases run git config --get-regex alias
. Here's what I have in my configuration:
alias.cleanup !git prune --progress && git gc --aggressive
alias.logd log --oneline --graph --decorate
alias.st status
alias.aliases config --get-regexp alias
alias.last log -1 HEAD
alias.pushdev !git checkout master && git merge dev && git push && git checkout dev
alias.fp !git fetch && git pull
alias.rv remote -v show origin
alias.list config -l
alias.files ls-files
alias.br branch --sort=committerdate
alias.sync pull upstream main && push origin main
You can see that I created an alias for this command.
PS C:\> git aliases
alias.cleanup !git prune --progress && git gc --aggressive
alias.logd log --oneline --graph --decorate
alias.st status
...
To create an alias, use the git config
command. I want to create an alias called lsc
that will list all cached files in the index, which you can do by running git ls-files --cached --full-name
. Here's how I would create the alias:
git config --global alias.lsc 'ls-files --cached --full-name'
I am creating a global alias called lsc
that will run the ls-files
command with the specified parameters. Now I can run git lsc
and see the same output as if I had run git ls-files --cached --full-name
.
PS C:\Scripts\PSProjectStatus> git lsc
.github/ISSUE_TEMPLATE/bug-report.yml
.github/ISSUE_TEMPLATE/config.yml
.github/ISSUE_TEMPLATE/feature-request.yml
.github/ISSUE_TEMPLATE/other-request.yml
License.txt
PSProjectStatus.psd1
PSProjectStatus.psm1
README.md
changelog.md
...
Here's a definition to open your git configuration in your default editor.
git config --global alias.edit 'config --global --edit'
You can also create aliases that accept parameters.
git config --global alias.cob '!git checkout -b $1 #'
The cob
alias will run git checkout
with the first parameter you provide. For example, git cob dev
will checkout the dev
branch. If it doesn't exist, it will be created.
PS C:\Scripts\GitDevTest> git cob dev
Switched to a new branch 'dev'
If you want to edit an alias, you can simply recreate it or manually edit your git configuration. Likewise, if you want to remove it you can edit the configuration or run a command like this:
git config --global --unset alias.lsc
This will remove the lsc
alias from the git
configuration.
If you have any useful git
aliases, I'd love to hear about them. Please share them in the comments.
Test-Path Tricks
I thought I knew all there was about the Test-Path
cmdlet. But I recently learned a few new tricks. And they aren't exactly hidden. I'm always reminding people to read the help and that should include me.
Test-Path
is very useful for checking the existence of a file or folder.\
PS C:\> $p = "c:\bar\foo.txt"
PS C:\> Test-Path -path $p
False
But maybe you are going to create that file and you want to validate that $p
is syntactically correct. You can use the -IsValid
parameter.
PS C:\> Test-Path -path $p -IsValid
True
Where you will encounter a problem is with path's that don't exist yet.
PS C:\> Test-Path -path Z:\bar\foo.txt
False
PS C:\> Test-Path -path Z:\bar\foo.txt -IsValid
False
The problem is that PowerShell has no way of knowing what provider to use to test the path for the Z: drive. It might be a registry path. You can work around this by specifying the provider.
PS C:\> Test-Path -path filesystem::Z:\bar\foo.txt -IsValid
True
PS C:\> Test-Path Registry::HKEY_Current_User\Software\FooBar -IsValid
True
There are a few other ways you might want to use Test-Path
. I encourage you to read the online help which contains more details and links.'
PS C:\> help Test-Path -online
Coming Up
If you will be in Bellevue next month for the PowerShell Summit, I hope you'll find me and say Hi. Next month we'll continue looking at creating WPF-based PowerShell scripts. Premium subscribers have full access to all previously published content so I hope you'll consider joining for a month or two to see what you've been missing. If you find this newsletter useful, I hope you'll recommend it to your friends and colleagues. Until next time, take care and keep on scripting!