I am Jakoby
I am Jakoby

Reputation: 617

How do I trim the output of a command?

I have the following code that grabs the full name associated with the microsoft account. It technically works but there should be a way to do it succinctly without redefining the variable.

$fullName = Net User $Env:username | Select-String -Pattern "Full Name";$fullName = ("$fullName").TrimStart("Full Name")

Upvotes: 0

Views: 772

Answers (1)

js2010
js2010

Reputation: 27423

Before trim:

net user $env:username | findstr 'Full Name'

Full Name                    The Admin

Using the version of foreach-object or % that runs a method with an argument. Note TrimStart() is case sensitive.

net user $env:username | findstr 'Full Name' | % trimstart Full` Name

The Admin

Select-object would have the string in a Line property.

net user $env:username | select-string 'Full Name' | % { $_.line.
  trimstart('Full Name') }

The Admin

Or take select-string's output in a string context:

net user $env:username | select-string 'Full Name' | 
  % { "$_".trimstart('Full Name') }

The Admin

Or just get-localuser and the fullname property:

localuser $env:username | % fullname

The Admin

Another demo of how trimstart works; each letter is taken by itself and the order doesn't matter:

'FFNNuuee  hi there' | % trimstart 'Full Name'

hi there

Upvotes: 1

Related Questions