Tools cover

jq: Validate JSON

Quick tip today. I wanted to validate my JSON file with jq. I think the common usage is to pipe text into jq but this is slow. jq has the ability to read the file directly, which is great if your json file is large. Next, we need to tell jq print out the loaded file. We use empty to not display any output. By default if there are any errors in the json file jq will output where the error is. ...

October 28, 2024
PowerShell cover

PowerShell: How to Create a File with Random Data

Generating random data into a file is important task that can be accomplished in PowerShell. Generating random data is useful for benchmarking reading and writing files. We will also do it fast! We can generate 1GB of random data into a file in about 800 ms. ⚠️ Do not use System.Security.Cryptography.RNGCryptoServiceProvider as it is obsolete, instead use System.Security.Cryptography.RandomNumberGenerator. System.Security.Cryptography.RandomNumberGenerator is good as we don’t have to create a new object and can use the static methods. ...

October 23, 2024
PowerShell cover

PowerShell: Output CSV From Two or More Arrays

The other day had created two arrays with the same length and I wanted to output them as columns in a CSV file. I couldn’t find an exact cmdlet to do this so I wrote a small script. Let’s get started. If you have two arrays such as: $arr1 = 'a', 'b', 'c', 'd' $arr2 = 1 , 2 , 3 , 4 I want to output the two arrays into a CSV such as each array is a column: ...

October 14, 2024
PowerShell cover

PowerShell: While Loop

PowerShell has verstile scripting language which includes different kinds of loops. The while loop is a common looping syntax found in different langauges. The basic syntax for a while loop is: while ( <Condition> ) { # Code } We can convert from a for loop to a while loop: # For-loop for (<Initialization>; <Condition>; <Repeat>) { # Code } # While-Loop <Initialization> while ( <Condition> ) { # Code <Repeat> } Example: Loop 10 times $i = 0 while ($i -lt 10) { Write-Output "Loop value = $i" $i++ } ℹ️ We can get the same behavior with a one liner for loop also: ...

October 7, 2024
PowerShell cover

PowerShell: How to use Where-Object

Where-Object is a cmdlet used for filtering objects in the pipe. Where-Object is placed in between commands and can remove objects that do not meet the criteria. Where-Object takes an object from the pipeline and filters all objects that do not meet the filter_expression. # General data flow: Cmdlet | Where-Object { filter_expression } | Cmdlet Where-Object returns the objects that match the specified condition. The filter includes (filter-in) the desired objects into the output. ...

October 4, 2024
PowerShell cover

PowerShell: How to Change Directory using Set-Location

PowerShell offers a few different ways to change the directory. The official cmdlet to change the directory is Set-Location and it has an aliases: cd, sl, and chdir. PowerShell’s Set-Location is more powerful than Command Prompt’s change directory cd. Set-Location can change directories, change to registry, and change to certificates, and change to environment variables. I will refer to the cmdlet as Set-Location but they can be swapped out with any of the aliases mentioned earlier. cd being common one used. ...

September 29, 2024
PowerShell cover

PowerShell: How to Create Multiple Files

When working with PowerShell, creating multiple files at once can be a convenient way to interact with the file system. In Linux bash, you can use the touch command to create multiple files using a single script line. However, when it comes to PowerShell, things get a bit more complicated. While there isn’t a direct equivalent to the touch command in PowerShell, we can still achieve the same result using PowerShell. We’ll explore various methods for creating multiple files in PowerShell, including using foreach, variables, and even leveraging WSL (Windows Subsystem for Linux) to run the touch command directly. ...

September 24, 2024
PowerShell cover

PowerShell: Git Diff Stash Ambiguous Argument Error

PowerShell is a powerful modern shell for Windows. However, it is not without its quirks. As a modern software developer on Windows you use PowerShell on a regular basis. Sometimes CLI applications (I’m looking at you git) interact with PowerShell in quirky ways. I saw this the other day with git in PowerShell. I was trying to git diff my current changes with the ones in my latest stash stash@{0}. However, when I ran the command git diff stash@{0}, I got the following error: ...

September 21, 2024
PowerShell cover

PowerShell: Select-String Only Return Result Matches

Select-String can be an alternative to Linux’s amazing and well known grep tool. However, grep must be downloaded and setup on the Windows system which might not be possible or convient to do so. A great built-in alternative on Windows PowerShell is Select-String which can do regular expression searching similar to grep. Goal is to have Select-String to only return the match text or the found reuslt. Quick Summary Select-String returns Matches and within it has Value which has the exact text match of the regex without any extra text: (cat .\results.txt | Select-String '<a.*?>.*?</a>').Matches.Value Example: curl a webpage, search a pattern Let’s say we curl’d a webpage and we want to extract the <a>...</a> tags. ...

September 15, 2024
PowerShell cover

PowerShell: Get Local IPv4 Address

Let’s get the local IP version 4 address of the system using PowerShell. We want the actual IPv4 address of the local system but not the localhost loopback address or the link-local address on the system. Link-local address is valid only for communications on local link (subnetwork) that our system is connected to. Link-local addresses are usually 169.254.0.0/16 (169.254.0.0 through 169.254.255.255). 1 Localhost is a hostname that refers to the current computer used to access it. The name localhost is reserved for loopback purposes. The IPv4 loopback address is 127.0.0.1. 2 ...

September 14, 2024
PowerShell cover

Find the Visual Studio Developer PowerShell script

Finding the Developer PowerShell launch script can be difficult as the location for it has change across the different Visual Studio versions (e.g., 2019, 2022), editions (e.g., Community, Professional, Enterprise), and installation location. Visual Studio 2022 and newer install the ‘Developer PowerShell for VS 2022’ as a Windows Terminal profile. This is the easiest way to access it. However, if you want to access it from a PowerShell ps1 script it is more complicated. Accessing via script is done typically if you have to run the script on another system where you don’t know which Visual Studio version/edition/location installed. ...

September 12, 2024
PowerShell cover

PowerShell: Invoke WebRequest Locally

In PowerShell, calling Invoke-WebRequest on a local share, such as \\mydirectory\myFile.txt, will result in an error message such as Invoke-WebRequest: The 'file' scheme is not supported.. If the file is on a network share, the best alternative to Invoke-WebRequest is to simply use Copy-Item. So: Invoke-WebRequest \\mydirectory\myFile.txt -OutFile myFile.txt becomes: Copy-Item \\mydirectory\myFile.txt C:\somewherelocally\myFile.txt I’ve seen this error with FTP also: Invoke-WebRequest: The 'ftp' scheme is not supported. Good news is that curl is shipped with Windows 1. So we can use curl for FTP files: ...

September 12, 2024
PowerShell cover

PowerShell: mkdir and New-Item Without Error

When creating and working with directories in PowerShell and Command Prompt, you may encounter errors when trying to create a new directory using the mkdir command. In best practice, we don’t want commands to throw exceptions or set error flags, as they can cause issues further down the script if not handled correctly. The best course of action is to prevent the error from occuring from the first place. In this article, we’ll go over how to prevent the errors. ...

September 12, 2024
PowerShell cover

PowerShell: Determine Version

Determining the PowerShell version is important for scripts as it tells the script the potential features available on the current PowerShell engine. The best way to do this is use the $PsVersionTable variable in the first method. In this example, the PowerShell version is 7.4.5. Method 1: Use $PSVersionTable Variable The most reliable and recommended way to determine the PowerShell version is using the built-in variable $PSVersionTable. $PSVersionTable contains the infromation about the PowerShell engine including the version. In the shell type $PSVersionTable and press enter. Note, if the variable doesn’t exist then the version is 1.0. ...

September 8, 2024
PowerShell cover

PowerShell: Aliases

The other day I was looking something up for PowerShell and realized that cat command for the classic ‘Windows Command Shell’1 is an alias of Get-Content PowerShell cmdlet. I wondered what other aliases does PowerShell have. There is an extensive collection of about 138 default aliases on PowerShell going to 109 PowerShell cmdlets on Windows. PowerShell was designed to work with other operating systems too, like Linux and MacOs. If you are new to PowerShell, then these aliases are a good place to learn commands that are often used. ...

September 6, 2024
PowerShell cover

PowerShell: Search Directory Names

Quick tip today. I wanted to find all directories with a certain name specifically .vs because I wanted to delete them. Is the .vs directory safe to delete? Yes, becuase its all generated files by VS that can be deleted. There is also db files in there that intellisense keeps, which can clutter your system after a while. To find all files: gci -recurse -Force -filter ".vs" gci alias for GetChild-Item -recurse to search all sub-directories -Force to search hidden and system files (this is useful for .vs directory because it is typically hidden) -filter to filter which directory or file we want To get the full path of each result: ...

August 7, 2023
PowerShell cover

PowerShell: Read and Edit XML

Create XML in PowerShell Before we start if we don’t have a XML file, we can create one: Set-Content .\test.xml '<?xml version="1.0" encoding="utf-8"?><root/>' Set-Content is a powershell cmd-let to write to a file. The first element is the prolog element 1: <?xml version="1.0" encoding="utf-8"?>. The W3C Recommendation describes the prolog as version number, encoding declaration. If we want to load it and write to it, it must have at least a root element, in this example it is <root> but it can be anything such as <catalog>, <company>, <store>, or even <zoo>. ...

August 5, 2023
PowerShell cover

PowerShell: Create Text File

Command Prompt to PowerShell To create a new text file using PowerShell, use New-Item. Classically, done by echo "some text" > into-a-file.txt, but PowerShell New-Item provides more robustness. The equvalent of the echo redirect > to a new file is PowerShell cmdlet New-Item. In this example, output hello into a new text file in the current directory called myFile.txt. echo hello > myFile.txt New-Item ".\myFile.txt" -Value "hello" -Force Note that New-Item does not replace the file if it already exists by default, we can use -Force to emulate the behavior of the echo redirect. If you run the command without -Force it will give error: New-Item: The file 'C:\myFile.txt' already exists. ...

June 1, 2023
PowerShell cover

PowerShell: Create Shortcut

How do you create a shortcut in Windows via scripts? Easy use PowerShell! Below is the script to create a shortcut named “MyShortcut”. One can additionally create shortcuts to URLs too. I commented everything so it is easy to follow. The only parameter you must set is the $Shortcut.TargetPath everything else is optional. The Windows Script Host (WSH) is a Windows administration tool. Within the Windows Script Host object model the root is Wscript and a sub-node which is WshShell (Wscript.Shell). WshShell object can run programs locally, modify the registry, create shortcuts, or access system folder. Specifically we’re interesting in creating shortcuts. We will access the WSH via the PowerShell script by utilizing the Windows Script Host (WScript) object (via COM) to create the shortcut. ...

May 23, 2023
PowerShell cover

PowerShell: Stop-Process to End Process in Windows

Fastest way to close a running executable in PowerShell is with Stop-Process (alias: kill). Stop-Process is a great tool for developers, especially when developing an application that sometimes doesn’t close properly, has stopped responding, are not shown on screen, or run multiple processes. Stop-Process PowerShell PowerShell 5 and newer can stop processes with Stop-Process (or alias kill) using the process name: Stop-Process -Force -Name 'ProcessName' For example, to close all instances of notepad.exe: ...

April 21, 2021