Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
March 26, 2024

Serialization Situation

One of PowerShell's greatest strengths, and one of its greatest challenges, is its object-oriented nature. Once you get your head wrapped around this concept, using PowerShell in the console or scripted automation begins to feel like magic. Where this becomes challenging is when you need to take output and save it to disk for future re-use. This process is know as serialization. In text-based situations this is easy.

git log | Out-File glog.txt
The output of the *git* command is text, so saving it to a file is simple. But what about PowerShell?
PS C:\> Get-WinEvent -LogName System -MaxEvents 3 | Out-File systemlog.txt
The file gets created.
   ProviderName: Microsoft-Windows-Time-Service

TimeCreated                     Id LevelDisplayName Message

-----------                     -- ---------------- -------

3/25/2024 9:59:16 AM           158 Information      The time provider '?

   ProviderName: Netwtw10

TimeCreated                     Id LevelDisplayName Message

-----------                     -- ---------------- -------

3/25/2024 9:51:13 AM          7003 Information      7003 - Roam Complete

3/25/2024 9:47:04 AM          6062 Warning          6062 - Lso was trig?
But the saved output is not very useful. And a lot of detail has been lost. Let's take a look at how to serialize PowerShell objects to disk in a way that preserves the object's structure and detail.
Get a premium subscription for full article and archive access

One of PowerShell's greatest strengths, and one of its greatest challenges, is its object-oriented nature. Once you get your head wrapped around this concept, using PowerShell in the console or scripted automation begins to feel like magic. Where this becomes challenging is when you need to take output and save it to disk for future re-use. This process is know as serialization. In text-based situations this is easy.

git log | Out-File glog.txt

The output of the git command is text, so saving it to a file is simple.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.