Replace characters in a file


  I have a huge flat file which has many fields.
Three of those fields are: First_Name, Last_Name and SSN (9 or <9  characters)
My requirement is to use powershell, to substitute the vowels in the First_Name and Last_Name, with 'X'
and change the SSN to XXX-XX-Last 4 SSN.

Can someone kindly help with the script for it?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

please post a representative sample of your CSV file.
ste5anSenior DeveloperCommented:
Just a comment: Depending on the use-case, this is may not a sufficient measure to anonymize data.
pvsbandiAuthor Commented:
Attached it.
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

That should do the trick.
Note that the last two SSNs don't comply with your definition; Brian Doug's only has 8 numbers, Kevin Brady's has 10. Is that an issue in the real file that you want to be informed about, or is it just because of the sample having been generated on the fly?
$ReplaceChar = 'X'
Import-Csv C:\Temp\Sample.csv | ForEach-Object {
	ForEach ($Property In 'First_Name', 'Middle_Name') {
		$_.$Property = $_.$Property -replace '[aeiou]', $ReplaceChar
	$_.SSN = $_.SSN -replace '(...)(..)(....)', ("$($ReplaceChar*3)-$($ReplaceChar*2)-`$3")
} | ConvertTo-Csv -NoTypeInformation |
	ForEach-Object {($_ -replace '","', ',').Trim('"')} |
	Set-Content -Path C:\Temp\Converted.csv

Open in new window

Edit: Fixed the issue where the characters in the SSN weren't replaced.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I recommend this simplification for the SSN substitution.  It produces valid output no matter the length of the SSN string.  You can use any string characters as the pad
	$a = ($_.SSN -replace '-', '').PadLeft(9, '.')
	$_.SSN = 'XXX-XX-' + $a.substring($a.length - 4)

Open in new window

pvsbandiAuthor Commented:
Thanks so much!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.