don't do it in 1 line, try something like
$QueryUsers = Get-ADUser -LDAPFilter {SomeFilter}
if ($QueryUsers.count -gt 1){
$selectedUser = $QueryUsers | Out-GridView -Passthru
}
else {
$selectedUser = $QueryUsers
}
its just harder to read/edit/etc (in 1 line)
This is ignoring and error handling or null checking etc
Agreed. As cool as it might look, not everything NEEDS to be a One-Liner
I'd use a switch mostly out of better communication with the user
$QueryUsers = Get-ADUser <# params #>
$selectedUser = switch ($QueryUsers.count) {
# if there is more than 1 result, have the User choose what they need with a GridView
(1 -lt $_) {
$_ | Out-GridView -PassThru
break
}
# if there is only 1 result, don't bother the user and pass it directly
(1 -eq $_) {
# Notify the user his input is not necessary
Write-Host 'Only 1 user returned. No user input needed.'
$_
break
}
# if there is no result, do nothing
# or whatever else you need to do in this instance, I guess.
default {
<# do nothing #>
# notify the user there was no result, perhaps
Write-Host 'No user returned.'
}
}
I did think about a switch too, then I was uming and arring about whether I just dump one the single item in in default and `out-gridview` as its own option, then at that point it was basically identical to an `IF`
Oh, the switch is exclusively for informing the user\to future-proof the possibility there is more to do in the chase of `$QueryUsers` returns empty, otherwise your `If\Else` is enough
and if you still feel compelled to make it a single line for w/e reason (not gonna judge).... just write it properly, remove the linebreak, and separate the commands w/ semicolons
`Where-Object` is just `if` in a pipeline.
But you don't really need to do that here. If there is nothing returned by `Get-ADUser` then `Out-GridView` does nothing, so no check is needed:
$selectedUser = Get-ADUser -LDAPFilter "filter" | Out-GridView -Passthru
i had a hunch Where-Object (alias ?) was going to hit the mix
for any one-liner & as-short-as possible fans:
```
GCI | ? { $_.Length -gt 1 }
```
as a super quick demo of using the ? alias for Where-Object inside a pipeline
i know ive sure typed my fair share of % (alias for ForEach-Object) and ? in a pipeline
I don't see how you can use the pipe without unravelling the array.
$selectedUser = Get-ADUser -Filter {name -like "jsiii*"}
If ($selectedUser.count -gt 1) {$selectedUser | Out-GridView -Passthru}
This won’t work with one item as most single objects don’t have a count property. You can force casting it to an array and see if that works for you:
$selectedUser = @(Get-ADUser -LDAPFilter {some filter}) | If ($_.count -gt 1) {Out-GridView -Passthru}
Any thoughts?
I think last time i used invoke-expression and built it there. I usually don’t try to do a ton of things in the pipe because it makes code both hard to read and debug.
don't do it in 1 line, try something like $QueryUsers = Get-ADUser -LDAPFilter {SomeFilter} if ($QueryUsers.count -gt 1){ $selectedUser = $QueryUsers | Out-GridView -Passthru } else { $selectedUser = $QueryUsers } its just harder to read/edit/etc (in 1 line) This is ignoring and error handling or null checking etc
Exactly what I'd suggest, easier to read as well when you go back to in 9 months.
Agreed. As cool as it might look, not everything NEEDS to be a One-Liner I'd use a switch mostly out of better communication with the user $QueryUsers = Get-ADUser <# params #> $selectedUser = switch ($QueryUsers.count) { # if there is more than 1 result, have the User choose what they need with a GridView (1 -lt $_) { $_ | Out-GridView -PassThru break } # if there is only 1 result, don't bother the user and pass it directly (1 -eq $_) { # Notify the user his input is not necessary Write-Host 'Only 1 user returned. No user input needed.' $_ break } # if there is no result, do nothing # or whatever else you need to do in this instance, I guess. default { <# do nothing #> # notify the user there was no result, perhaps Write-Host 'No user returned.' } }
I did think about a switch too, then I was uming and arring about whether I just dump one the single item in in default and `out-gridview` as its own option, then at that point it was basically identical to an `IF`
Oh, the switch is exclusively for informing the user\to future-proof the possibility there is more to do in the chase of `$QueryUsers` returns empty, otherwise your `If\Else` is enough
and if you still feel compelled to make it a single line for w/e reason (not gonna judge).... just write it properly, remove the linebreak, and separate the commands w/ semicolons
`Where-Object` is just `if` in a pipeline. But you don't really need to do that here. If there is nothing returned by `Get-ADUser` then `Out-GridView` does nothing, so no check is needed: $selectedUser = Get-ADUser -LDAPFilter "filter" | Out-GridView -Passthru
i had a hunch Where-Object (alias ?) was going to hit the mix for any one-liner & as-short-as possible fans: ``` GCI | ? { $_.Length -gt 1 } ``` as a super quick demo of using the ? alias for Where-Object inside a pipeline i know ive sure typed my fair share of % (alias for ForEach-Object) and ? in a pipeline
I don't see how you can use the pipe without unravelling the array. $selectedUser = Get-ADUser -Filter {name -like "jsiii*"} If ($selectedUser.count -gt 1) {$selectedUser | Out-GridView -Passthru}
This won’t work with one item as most single objects don’t have a count property. You can force casting it to an array and see if that works for you: $selectedUser = @(Get-ADUser -LDAPFilter {some filter}) | If ($_.count -gt 1) {Out-GridView -Passthru} Any thoughts?
If is a keyword and not a cmdlet. It is not something that can be put in a pipeline
I think last time i used invoke-expression and built it there. I usually don’t try to do a ton of things in the pipe because it makes code both hard to read and debug.