0

I'm new to PowerShell and it would be much appreciated if I could get some expertise assistance with a script that I'm trying to write in PowerShell.

Objective: Run a script to remotely check the name of the current user who is logged on to that machine.

Current script:

This line with the variable of $EnterComputerName prompts us to input in what computer name we want to search.

$EnterComputerName = Read-Host -Prompt "Enter Computer name"

This line searches the details from that specified computer and pipe it so that it will only show the Username property.

$Name = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $EnterComputerName | Select-Object Username

Problem: The result of the above line only outputs the user ID instead of their actual name, example: domain\N12345

I want to use the following line to convert userID into name:

Get-ADUser $Name | Select-Object GivenName, Surname 

However, Get-ADUser only recognizes 'N23705' instead of 'domain\N23705'. Is there a way I can shorten this to 'N23705' and pass that value to the $Name variable?

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
KeijiVBoi
  • 5
  • 1
  • 1
    Does this answer your question? [Pull NT user ID from powershell](https://stackoverflow.com/questions/25693818/pull-nt-user-id-from-powershell) or [Store username of system](https://stackoverflow.com/q/20749920/150605) – Lance U. Matthews Feb 06 '20 at 04:24

1 Answers1

1

One simple way to accomplish this is to use replace to remove the unwanted domain prefix.

$Name = (Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $EnterComputerName).UserName -replace ".*\\", ""

Edit: removed Parenthesis in replace syntax