

Quick Tip - Did the last command work or not?
source link: https://thomasrayner.ca/quick-tip-did-the-last-command-work-or-not/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Quick Tip - Did the last command work or not?
In PowerShell, there is usually at least a few ways to do most tasks and detecting if the last command resulted in an error or if it worked is no exception. You could wrap code in a try/catch block, but sometimes that’s overkill. Regardless of your reason for wanting to get the work/borked status of the last command, here are a couple simple ways of doing it.
The Get-History cmdlet is a great way to get this and other information for commands you executed in your current session. If you run it, you’ll get an ID and a “CommandLine” which is the command that you ran, but if you pipe into Select-Object -Property *, you’ll see that there’s also an ExecutionStatus and times for start and end. That ExecutionStatus is what you’re looking for in this case.
If you just run Get-History | Select * then you’ll get those pieces of information for every command you ran in your current session. If you only want to see the information for the last command you ran, just remember that you’re working with an array and run (Get-History)[-1] | Select *. That will get the last item in the array of command information returned by Get-History and show you that info.
If you truly just want to know if the last command completed successfully or not, there’s a special variable built into PowerShell for it. It’s $?. Just type $? into your console and you’ll get back either True or False, a boolean value for if the last command completed successfully or not. Pretty handy shortcut, no?
Recommend
-
21
Quick Tip: Simulate a bad network connection on macOS (and Linux)2020-06-19Yesterday, I got a tip from a colleague to fake a poor network connection (for debugging an issue) with tc (
-
10
Quick Tip: GitHub Action for pushing docker images2020-06-16Here is a full example of using the official Docker build-push-action for GitHub to publish directly to th...
-
15
Quick Tip - See All The Tab-Completion Options At Once In The PowerShell Console If you’re used to working in VS Code or the PowerShell ISE, you’ve undoubtedly enjoyed intellisense which is the feature that shows you all...
-
20
Quick Tip - Split A PowerShell Collection Into Two Arrays Did you know that you can use Where-Object to split a collection into two arrays? Like, if you had an array containing the numbers 1 to 10, you c...
-
13
Quick Tip - Re-Run The Last Command Sometimes, while you’re poking around in the console, you want to re-run the last command. Sure, you can hit the up arrow and enter, but PowerShell always gives you multiple ways to do...
-
7
Quick Tip - PowerShell Supports Partial Parameter Names Did you know that PowerShell supports the usage of partial parameter names? This isn’t such a big deal since tab completion is a thing… and if you’re writing code,...
-
16
Quick Tip - Update a Tag on an Azure Resource Working with Azure resources can be a bit of an adventure sometimes. Say you want to update a tag on an Azure resource. Not remove it, but change its value. If you try to add...
-
18
Quick Tip - Open A File In Default Program When you double click a file in Explorer.exe, it automatically opens in its default program if it has one associated with its type. But did you know you can do the same thing us...
-
13
Quick Tip - Copy The Output Of The Last PowerShell Command To Clipboard I recently found myself poking around in PowerShell and going “oh, good now I want to copy and paste that output into an email/dialog box/tweet/note...
-
9
Quick Tip - String Manipulation - First Name Last Name to Last Name, First Name I’ve got kind of a silly post this week. I often get a list of names in the format… John Doe Jane Doe Mik...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK