1

How to split a string by spaces in a Windows batch file? didn't work for me.

I issue a Windows command to list the session ID of the logged in user, like this

PS C:\> $id = (quser /server:'ServerName' | Where-Object { $_ -match 'User' })
PS C:\> echo $id
User         console       4  Active none    6/13/1023 9:00 AM

How can I get the session id "4" from the output? I tried

PS C:\> $id = (quser /server:'ServerName' | Where-Object { $_ -match 'User' }).split(' ')[2]

but $id is just a blank string.

TIA

3 Answers 3

1

With hard-coded

$id = 'User         console       4  Active none    6/13/1023 9:00 AM'

First, check all split syntax variants:

$id.Split
OverloadDefinitions
-------------------
string[] Split(Params char[] separator)
string[] Split(char[] separator, int count)
string[] Split(char[] separator, System.StringSplitOptions options)
string[] Split(char[] separator, int count, System.StringSplitOptions options)
string[] Split(string[] separator, System.StringSplitOptions options)
string[] Split(string[] separator, int count, System.StringSplitOptions options)

Then, use

$id.Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries)
User
console
4
Active
none
6/13/1023
9:00
AM

Without additional parameters, $id.Split(' ').Count gives 26 while
$id.Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries).Count -> 8

Sign up to request clarification or add additional context in comments.

Comments

1

A PowerShell-idiomatic solution is to use the unary form of the -splitoperator, which behaves as follows:

  • Leading and trailing whitespace is ignored.
  • Any nonempty run of whitespace - no matter how long - is treated as a single separator.
$id = (-split (quser /server:'ServerName' | Where-Object { $_ -match 'jdoe' }))[2]

Note: You'll probably want to make the -match operation more robust, such as with -match '\bjdoe\b'


A simpler example:

-split '  one          two  three ' | ForEach-Object { "[$_]" }

Output (prints the resulting tokens enclosed in [...]):

[one]
[two]
[three]

Note that there are reasons to generally prefer the -split operator to the .Split() .NET string method, as discussed in this answer.

Comments

0

in my opinion, in Windows cmd, you can use the FOR /F command to split a string and get the nth token (substring). Below is an example:

Suppose you have the following string: "This is a test string" and you want to get the 3rd word (which is "a" in this case). You could do:

@echo off
setlocal enabledelayedexpansion
set str="This is a test string"
for /f "tokens=3 delims= " %%a in ("%str%") do set nth_word=%%a
echo !nth_word!

In this script, tokens=3 specifies to get the third word, delims= sets the delimiter to a space (which is the separator in this case), %%a is the variable that will hold the value of the third word, "%str%" is the string to split, set nth_word=%%a sets the value of %%a to the variable nth_word, and echo !nth_word! prints out the value of nth_word.

1 Comment

Thanks, but I tagged this as a Powershell question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.