Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Task, get the ID from the query user command, I'm trying to get the ID value from the the command 'query user',

Example:PS>  query user

 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME

>administrator         rdp-tcp#0           3  Active          .  04/25/2013 08:43

I'm trying to use psexec and attach to the session.

psexec \\\pc -i sessionid somecommand

how would I go about getting the ID and only the ID from the above query? this is what I've tried so far, among other things...

PS>  query user |Select-Object $_.ID

USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME

>administrator         rdp-tcp#0           3  Active          .  04/25/2013 08:43

I would think this is easy but apparently I am having a brain fart, ideally I like to do this:

$IDValue = query user | Get_the_ID_somehow

psexec \\\pc -i $IDValue somecommand..

thanks in advance.

share|improve this question
    
You migtt take a look at the somewhat related question here. serverfault.com/questions/347723/… –  Zoredache Apr 25 '13 at 18:38
    
sort of helpful but I still can't get the ID from the results. –  user219872 Apr 25 '13 at 20:32
    
@Zoredache I'm thinking I should use 'query user' then I'd have one line to parse. –  user219872 Apr 25 '13 at 21:00
    
this could work, $s = query user $id =$s[1].Substring(39,5) –  user219872 Apr 25 '13 at 21:04

2 Answers 2

I believe you're looking for $IDValue = query user | Select-Object ID

share|improve this answer

Since "query user" is not an object you cannot use it as an object in powershell unless you contruct a custom PSObject, so

$IDValue = query user | Select-Object ID
will not work.

I would use something like this:

$users = query user
$IDs = @()

For($i=1;$i -lt $users.Count;$i++){
'#using regex to find the IDs`
$temp = [string]($users[$i] | Select-String -pattern "\s\d+\s").Matches
$temp = $temp.Trim()
$IDs += $temp
}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.