Add a collumn in output result using powershell

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Magskull Magskull 2 weeks, 6 days ago.

  • Author
    Posts
  • #58186
    Profile photo of Magskull
    Magskull
    Participant

    I need add a column in a output of command.

    The comand run on a terminal service and get the status of users

    $users = Invoke-Command -ScriptBlock {query user}
    
    $users | Out-File c:\users.txt

    The output of "query user" is:

    USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
    user1 rdp-tcp#1 272 Active 3 18/11/2016 04:09
    user2 rdp-tcp#24 277 Active 1:59 18/11/2016 06:05

    But I need include a collumn with the datetime when command is run, see:

    USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME EXEC TIME
    user1 rdp-tcp#1 272 Active 3 18/11/2016 04:09 11:00
    user2 rdp-tcp#24 277 Active 1:59 18/11/2016 06:05 11:00

    I have tried to use "$users | Add-Member NoteProperty IName($fiel.Hour)" in command, but not works....

  • #58196
    Profile photo of random commandline
    random commandline
    Participant
    $scriptblock = 
    (query user) -replace '\s{2,}',',' | ConvertFrom-Csv | 
    Select-Object *,@{n='Exec Time';exp={Get-Date}} 
    
    $users = Invoke-Command -ScriptBlock {$scriptblock}
    $users | Out-File c:\users.txt
    
  • #58204
    Profile photo of Magskull
    Magskull
    Participant

    Yes, perfect.

    I make a change on the script

    $scriptblock = 
    (query user)  | ConvertFrom-Csv | 
    Select-Object *,@{n='Exec Time';exp={Get-Date}} 
    
    $users = Invoke-Command -ScriptBlock {$scriptblock}
    $users | Out-File c:\users.txt

    Because I need the file continue with the original layout, and I want to write the results bellow the previous result in txt file.

You must be logged in to reply to this topic.