Do note that this causes conversion behind the scenes (using indexing, from a 
string to an array of characters). As jlc points out, Substring is the "right" 
way to do it.

$username.SubString( 0, 1 )

From: [email protected] [mailto:[email protected]] On 
Behalf Of Joseph L. Casale
Sent: Friday, August 23, 2013 7:29 AM
To: '[email protected]'
Subject: RE: [NTSysADM] PowerShell string manipulation

Sure,
Powershell happily supports duck typing as '[]' notation indexes an array.

PS > $username |gm |select-String Substring
string Substring(int startIndex), string Substring(int startIndex, int length)

Substring() is a string method, just to be pedantic:)
jlc


From: [email protected]<mailto:[email protected]> 
[mailto:[email protected]] On Behalf Of Tony Patton
Sent: Friday, August 23, 2013 5:11 AM
To: [email protected]<mailto:[email protected]>
Subject: Re: [NTSysADM] PowerShell string manipulation

I just tried this and this worked:

$username = "pattont"
$username[0]

This returned "p".

Results may vary though.

T

On 23 August 2013 11:03, James Rankin 
<[email protected]<mailto:[email protected]>> wrote:
Anyone have any ideas how to grab the first character of a string (namely a 
username) in PowerShell?
Plenty of hits on removing the last characters from Google, but nothing useful
Cheers,



--
James Rankin
Technical Consultant (ACA, CCA, MCTS)
http://appsensebigot.blogspot.co.uk


Reply via email to