Share via


Powershell - Get Capital Letters from a string

Question

Friday, September 12, 2014 5:46 PM

Hi there

Im doing a script to get the content of a text file which helds several names in a variable called $names.
After that, i need to get the capital letters only and put this in another variable, which will be called $id.

I.e.: $names = John Wayne

$id must be JW 

$names = Peter of Griffin

$id must be PG

Any tip?

Thanks!

João

All replies (3)

Friday, September 12, 2014 5:57 PM ✅Answered | 3 votes

$Names = 'John Wayne','Peter of Griffin'

$Names -creplace '[^A-Z]'

JW
PG

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Friday, September 12, 2014 5:55 PM

You could use regex

('John Wayne' | select-string -CaseSensitive "[A-Z]" -allmatches).matches.value -join ""

Friday, September 12, 2014 9:17 PM

Nice reply.

Just exactly what I was looking for: clean e clear.

Thanks.

João