

Quick Tip - Split A PowerShell Collection Into Two Arrays
source link: https://thomasrayner.ca/quick-tip-split-a-powershell-collection-into-two-arrays/
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 - 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 could split it into one array of even numbers, and another array of odd numbers? It’s pretty cool. Thanks Herb Meyerowitz for this tip!

As you can tell from Herb’s comment in this screenshot, it’s actually the .where() method that is relatively new that we’re using to split collections this way. The syntax is kind of atypical, so let’s break it down.
$a, $b = (1..5).where({$_ % 2}, 'split')
- $a, $b =
- This part is creating two variables, named a and b that will be used to contain the output of our splitting activity.
- (1..5)
- This just creates an array of the numbers 1 through 5.
- .where( )
- This is a method that comes with PowerShell 5 (I'm pretty sure) which works mostly like the Where-Object cmdlet that you're used to. Most people just use it to filter collections on a filterscript (stay tuned) but it also takes other arguments.
- {$_ % 2}
- This is the filterscript, or basically the criteria we're using to split up our collection. It will effectively create a true and a false list like the condition in an if statement. The percent sign is the modulus operator and will determine if the number is divisible by two without a remainder or not.
- 'split'
- This is the mode. By default, the collection is filtered using the filterscript like when using Where-Object and only the objects matching the condition are returned. But that's not the only mode! We're going to use the split mode to break it into two collections. Maybe another blog post will come out on some of the other modes.
That’s it!
Recommend
-
14
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...
-
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,...
-
15
Quick Tip - Use PowerShell To See How Many Files Are In A Directory Here’s a way to see how many files are in a directory, using PowerShell. As you likely know, you can use Get-ChildItem to get...
-
10
Quick Tip - Diagnosing Slow PowerShell Load Times I could write an entire book on “why does my PowerShell console take so long to load?” but I don’t want to write that book. Instead, here’s a way to make sure the reason...
-
20
Quick Tip - Create New LPR Printers Using PowerShell There are a bunch of overloads for Add-Printer and Add-PrinterPort to accommodate different kinds of printers and ports. I found it t...
-
14
Quick Tip - Use PowerShell To Detect If A Location Is A Directory Or A Symlink In PowerShell, symbolic links (symlinks) appear pretty transparently when you’r...
-
11
Sometimes we have one array that we might want to split into multiple arrays. Here’s a quick and easy way to do that. The Problem Let’s say we have the following array: const nums = [1, 2, 3, 4, 5, 6, 7, 8,...
-
7
29 Jul 2018 software-dev
-
15
A quick PowerShell tip I have a bunch of PowerShell functions that I stick in my $profile file. Simple stuff, things to make my day to day development work easier. With my sieve-like memory, I need a quick way...
-
9
JS Tip Series: Use Objects than Arrays For Large Data Most of the time, we have been using data in an array type and we have been us...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK