3

I have a weird behavior with format-list. When I paste the following code directly into the shell - everything works like a charm:

    @("Administrator","SomeUser","SomeOtherUser") |% {
        $uname = $_;
        $u = gwmi win32_useraccount |? { $_.Name –eq $uname }
        if (-not $u) {
            write-host ("[-] "+ $uname + " does not exist!")
        } else {
            write-host ("[+] "+ $uname + ":")
            $u
        }
    }

    @("Administrator","SomeUser","SomeOtherUser") |% {
        $uname = $_;
        gwmi win32_groupuser -computer . | select GroupComponent,PartComponent |? { $_.PartComponent -match ",Name=`""+$uname+"`""} | fl *
    }

However, when I put the same code in a function, say test, and call test, PowerShell drops an error about format-list. I cannot figure out why - maybe I have been searching on the wrong end, but I did not find anything.

    function test {
        @("Administrator","SomeUser","SomeOtherUser") |% {
            $uname = $_;
            $u = gwmi win32_useraccount |? { $_.Name –eq $uname }
            if (-not $u) {
                write-host ("[-] "+ $uname + " does not exist!")
            } else {
                write-host ("[+] "+ $uname + ":")
                $u
            }
        }

        @("Administrator","SomeUser","SomeOtherUser") |% {
            $uname = $_;
            gwmi win32_groupuser -computer . | select GroupComponent,PartComponent |? { $_.PartComponent -match ",Name=`""+$uname+"`""} | fl *
        }
    }

The error message displayed is:

out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. This is likely caused by
a user-specified "format-list" command which is conflicting with the default formatting.
+ CategoryInfo : InvalidData: (:) [out-lineoutput], InvalidOperationException
+ FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand

A screenshot of the issue:

Enter image description here

1 Answer 1

7

A function is supposed to return one or more Objects, not formatted output data (which is intended for the host/screen).

In other words, don't use Format-* cmdlets inside a function

Simply remove |fl * from the last statement, and pipe the output of the test function call to Format-List instead:

function test {
    @("Administrator","SomeUser","SomeOtherUser") |% {
        $uname = $_;
        $u = gwmi win32_useraccount |? { $_.Name –eq $uname }
        if (-not $u) {
            write-host ("[-] "+ $uname + " does not exist!")
        } else {
            write-host ("[+] "+ $uname + ":")
            $u
        }
    }

    @("Administrator","SomeUser","SomeOtherUser") |% {
        $uname = $_;
        gwmi win32_groupuser -computer . | select GroupComponent,PartComponent |? { $_.PartComponent -match ",Name=`""+$uname+"`""}
    }
}

test |fl *

Along the same lines, at least for writing reuseable functions:

You might also benefit from using the -Query parameter with Get-WmiObject, and have WMI do the filtering instead of returning all users to powershell and then filtering them

Get-WmiObject -Query "SELECT * FROM Win32_UserAccount WHERE Name = '$uname'"
2
  • Thank you very much, that helps a lot - PLUS thank you for the style comments. I admit my script is rather quick and dirty plus i don't script PowerShell that often.
    – go chilla
    Jul 31, 2014 at 11:39
  • You're most welcome :-) A verbose style is especially handy when it's to be maintained by people who don't look at PowerShell regularly Jul 31, 2014 at 15:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .