Good morning all!
I've been messing around with the switch statement since I found out about it on another post I made.
I have this issue with the code below, where its printing multiple rows with the same information and, I get why its doing so but, I dont know how to fix it. I believe its messing up when im assigning the variable but, im not too sure. Can someone point me in the right direction on what may be causing the issue? Any help is appreciated.
$gc = Get-ChildItem -Path 'C:\users\abrah\OneDrive\Desktop'
Foreach ($File in $gc) {
switch -Wildcard ($file) {
"deskt*" { $Desk = "This is the location: $($File.FullName)" }
"*v*" { $VA = "This is the location: $($File.FullName)" }
}
$VCount = $va | Measure-Object | Select-Object -ExpandProperty Count
$Dcount = $Desk | Measure-Object | Select-Object -ExpandProperty Count
$PS = [pscustomobject]@{
DesktopLocation = $Desk
DCount = $Dcount
VLocation = $VA
VCount = $VCount
}
$PS
}
About the script: Im just looking to find any files on my desktop that begin with deskt, and any with the letter V in it. Then im throwing it into a custom object while attempting to count how many files contain those key letters.
Here are the results btw:

As for your switch-statement-based approach:
switch itself is capable of processing collections, so there's no need to wrap it in a foreach loop.
What you're looking for is to build up two collections from the input, which requires you to:
$Desk and $VA as a collection data type.switch branch handlers.# Initialize the collections.
$Desk = [System.Collections.Generic.List[string]] @()
$VA = [System.Collections.Generic.List[string]] @()
# Make `switch` loop over the .FullName values of all items in the target dir.
switch -Wildcard ((Get-ChildItem C:\users\abrah\OneDrive\Desktop).FullName) {
'*\deskt*' { $Desk.Add("This is the location: $_") } # add to collection
'*\*v*' { $VA.Add("This is the location: $_") } # add to collection
}
# Construct and output the summary object
[pscustomobject] @{
DesktopLocation = $Desk
DCount = $Desk.Count
VLocation = $VA
VCount = $VA.Count
}
Note:
While it is possible to use an array as the collection type, "appending" to arrays with +=, while convenient, is inefficient, because a new array must be created behind the scenes every time, given that arrays are immutable with respect to their element count.
While it may not matter for arrays with only a few elements, it's a good habit to form to use System.Collections.Generic.List`1as an efficiently extensible collection type.
That said, given that statements such as switch and foreach loops can act as expressions when assigned to a variable, if all output is to be captured in a single collection, you don't even need a collection type explicitly, which is both concise and efficient; e.g.:
$collection = foreach ($i in 0..2) { $i + 1 } stores array 1, 2, 3 in $collection; note that if only one object is output, $collection will not be an array, so to ensure that you can use [array] $collection = ...
Alternatively, a simpler solution is to take advantage of the fact that wildcard-based filtering via the -Filter parameter is fast, so that even calling Get-ChildItem twice likely won't be a problem:
$dir = 'C:\users\abrah\OneDrive\Desktop'
[array] $Desk = (Get-ChildItem -LiteralPath $dir -Filter deskt*).FullName
[array] $VA = (Get-ChildItem -LiteralPath $dir -Filter *v*).FullName
[pscustomobject] @{
DesktopLocation = $Desk
DCount = $Desk.Count
VLocation = $VA
VCount = $VA.Count
}
You don't need a switch or a foreach loop to count how many files matching each pattern you have:
# Fetch files
$gc = Get-ChildItem -Path 'C:\users\abrah\OneDrive\Desktop'
# Count those starting with "Deskt"
$desktopFiles = $gc |Where-Object Name -like "Deskt*"
# Count those matching `*v*`
$vFiles = $gc |Where-Object Name -like "*v*"
# Output details
[PSCustomObject]@{
DesktopLocations = @($desktopFiles.FullName)
DesktopCount = $desktopFiles.Count
VLocations = @($vFiles.FullName)
VCount = $vFiles.Count
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With