user3415777
user3415777

Reputation: 77

powershell script delete first character in output

How would I delete the first line in the output from this command? If the output is A123456, I just want it to show me 123456 with the A.

Get-User $_ | Select sAMAccountName

Upvotes: 5

Views: 31474

Answers (4)

Ken
Ken

Reputation: 17

$str = "@123456";
$str = $str.Substring(1,($str.Length-1));
$str;

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

You can remove the first character with the -replace operator:

(Get-User $_ | select -Expand sAMAccountName) -replace '^.'

Or

Get-User $_ | select @{n='sAMAccountName';e={$_.sAMAccountName -replace '^.'}}

if you want to keep objects rather than strings.

Upvotes: 2

jon Z
jon Z

Reputation: 16616

Substring(1) returns a substring containing all chars after the first one.

Get-User $_ | Select @{N="myAccountName";E={$_.sAMAccountName).substring(1)}}

Upvotes: 2

Frode F.
Frode F.

Reputation: 54881

Just get the substring starting at the second letter(index 1).

Get-User $_ | Select @{n="AccountName";e={$_.sAMAccountName.Substring(1)}}

If you just need the value, you could do it like this:

Get-User $_ | % { $_.sAMAccountName.Substring(1) }

Upvotes: 5

Related Questions