Split text by columns in PowerShell


I’m a PowerShell novice (Bash is my thing normally) who’s currently trying to obtain qwinsta output to show who is logged in as an ‘rdpwd’ (rdesktop) user so that I can check each username against a list of usernames, and if they do not match, log them off.

I am currently working through two problems:

  1. I am unable to split the qwinsta output to be left with only the username – I’ve tried the “split” function but so far am getting either syntax issues or weird results; one gripe seems to be that ‘\s+’ matches the letter S instead of whitespace; other times I’ve managed to split to the second column, but only output from line 1 appears
  2. Whilst I’m not there yet, I sense that I will have problems with the second step as well, namely looping through the array of non-log-offable users (which are to be obtained from a local user group)

I’ll focus on problem 1 for now!

The text I’ve got is:

The output I want is:

(With the aim of then creating a loop that says, in brief terms, “foreach user in list, if not in localgroup, logoff user”.)

So far, I’ve got as far as selecting text with ‘rdpwd’, but using all manner of variations on “split”, I have not got further forward than that.

I’m happy to share what I’ve got already, but alas I don’t think it’ll help anyone!

Any assistance would be most appreciated. 🙂


Honestly I’d look up a better way to do this, but you can fudge it with some text manipulation and the ConvertFrom-Csv cmdlet:

Firstly replace any leading spaces or > characters with nothing, then replace any white spaces with a comma. Then you can pipe to ConvertFrom-Csv and work with the data as an object.


Actually, the above has some issues, mostly with the \s+ because if a column is blank it does not get correctly recognised as a blank field, and the next text is incorrectly promoted to the current field.

The below is a full blown parser for this command, and would probably work for any sort of tabulated output from a native windows exe:


Split text by columns in PowerShell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply