0

Currently I use this to get the username of the remote system:

Get-WmiObject win32_computersystem -Computer $tag1 | Format-Table -Property @{Name="Username";Expression={$_.username}} -Autosize;

This will output:

Username
--------
GHS_NTDOMAIN\AJSTEPANIK

I want to store the AJSTEPANIK part to a variable so I can use it in another part of the script, but I am unsure how to trim it or if there is another command to get just the name.

karel
  • 5,489
  • 46
  • 45
  • 50
Aaron
  • 3,135
  • 20
  • 51
  • 78
  • You can also take a look [here](http://technet.microsoft.com/en-us/library/ee692804.aspx) for examples of string manipulation in PowerShell. – Roman Dec 23 '13 at 19:31

1 Answers1

2

You'll want to split the string into two parts (the domain and the username), using the backslash as a delimiter. That will split the string into an array that looks like this:

array[0] = "GHS_NTDOMAIN"

array[1] = "AJSTEPANIK"

We'll be taking the username portion of the array, and storing it in a variable, like so:

$userName = (Get-WmiObject win32_computersystem -Computer $tag1).UserName.Split("\")[1]
mikekol
  • 1,782
  • 12
  • 14
  • 1
    I would actually recommend using the `-1` index, as sometimes you might get a result in the format of `Computername\Domain\Username`. With `-1` you will always get the Username – Frode F. Dec 23 '13 at 22:06