HelpfulAdvisor
asked on
How would I change the case of a string to a proper case?
Hi Experts,
I'm new to PowerShell scripting, and I'm trying to convert data in a CSV file to proper case for users' first name and last name. I don't want to create new variable lines each time I want to do this, so I was wondering how to modify this line to automatically change the case to proper case when it is called later in the script.
Here is the variable I want to modify to output the result in proper case:
Some of the names in the CSV file are in all upper case for the columns "sn" and "givenName". I'd like to convert them to proper case, so that when later in the script the results are placed into my create user functions, it will appear as normal.
Hoping it's a simple solution without having to write additional variables and call them something else. Thanks in advance!
I'm new to PowerShell scripting, and I'm trying to convert data in a CSV file to proper case for users' first name and last name. I don't want to create new variable lines each time I want to do this, so I was wondering how to modify this line to automatically change the case to proper case when it is called later in the script.
Here is the variable I want to modify to output the result in proper case:
$displayName=$sn + ", " + $givenName
Some of the names in the CSV file are in all upper case for the columns "sn" and "givenName". I'd like to convert them to proper case, so that when later in the script the results are placed into my create user functions, it will appear as normal.
Hoping it's a simple solution without having to write additional variables and call them something else. Thanks in advance!
ASKER
Hi KenMcF!
Thanks for your quick reply. I replaced my original variable with your line, and it didn't seem to change to case of the text.
Would there be another thing to try, or perhaps a reason why it's not working when perhaps it should?
Thank you in advance.
Thanks for your quick reply. I replaced my original variable with your line, and it didn't seem to change to case of the text.
Would there be another thing to try, or perhaps a reason why it's not working when perhaps it should?
Thank you in advance.
Can you post your full code
also i see when I postted it added an extra $displayname = so try this one.
also i see when I postted it added an extra $displayname = so try this one.
$displayname = "$((Get-Culture).textinfo.totitlecase($sn)), $((Get-Culture).textinfo.totitlecase($givenname))"
Am I right in understanding you want to return firts letters in caps and rest lower case in all the words that make up the display name?
In this case I would use a function like this one:
After the function code you can call the Function easily:
Result:
Some Words Here
In your case, with my function at the top of your script you call this:
Hope this helps?
In this case I would use a function like this one:
Function Convert-WordsToProperCase
{
param
(
$Word
)
#Split the string in spaces
$WordSplit = ($word.Split(" "))
#Count the number of words in the string
$WordCount = $WordSplit.Count
#Loop through each word
for ($i = 0; $i -lt $WordCount; $i++)
{
#Get the current word
$CurrentWord = $WordSplit[$i]
#Set the first character in the word to UPPER case. The rest to LOWER case
$CurrentWordCharUpper = ($CurrentWord.SubString(0, 1)).ToUpper()
$CurrentWordCharLower = ($CurrentWord.SubString(1, ($CurrentWord.Length - 1))).ToLower()
#String the word together
$NewCurrentWord = $CurrentWordCharUpper + $CurrentWordCharLower
#Add the word to the concat string var
$WordConcat += $NewCurrentWord + " "
}
#Take the space off the end
$WordConcat = $WordConcat.SubString(0, $WordConcat.Length - 1)
#Return the word with caps changed accordingly
return $WordConcat
}
After the function code you can call the Function easily:
Convert-WordsToProperCase "SOME words HERE"
Result:
Some Words Here
In your case, with my function at the top of your script you call this:
$displayname = Convert-WordsToProperCase ($sn + ", " + $givenName)
Hope this helps?
ASKER
Hi KenMcF,
I tried your line of code, and unfortunately, it didn't seem to format the text, so I'm wondering if I'm doing something wrong. Here's my code I'm using
Hi chrismerritt, thanks for jumping in to lend a hand. I'll give your code a try, but was hoping it could be a one-liner as this is to create users for AD accounts. We're quickly developing a series of scripts as a last-second requirement for a project we're on, and so I wanted to see if we could keep it as simple as possible. However, I'll cut and paste your code when I have a bit more time to see if it will do the trick.
I appreciate your help with this, and will let you know how it works out.
I tried your line of code, and unfortunately, it didn't seem to format the text, so I'm wondering if I'm doing something wrong. Here's my code I'm using
$objOU=[ADSI]"LDAP://OU=Staging,OU=Country,OU=LA,DC=mylab,DC=local"
$dataSource=import-csv "userlist.csv"
foreach($dataRecord in $datasource) {
$cn=$dataRecord.givenName + " " + $dataRecord.sn
$sAMAccountName=$dataRecord.givenName + "." + $dataRecord.sn
$givenName=$dataRecord.givenName
$sn=$dataRecord.sn
$sAMAccountName=$sAMAccountName.ToLower()
$displayname = "$((Get-Culture).textinfo.totitlecase($sn)), $((Get-Culture).textinfo.totitlecase($givenname))"
$userPrincipalName=$sAMAccountName + "@pumalab.local"
$physicalDeliveryOfficeName=$dataRecord.physicalDeliveryOfficeName
$title=$dataRecord.title
$telephone=$dataRecord.telephoneNumber
$department=$dataRecord.department
$email=$dataRecord.mail
$location=$dataRecord.l
$street=$dataRecord.street
$postalcode=$dataRecord.postalCode
$mobilephone=$dataRecord.mobile
$country=$dataRecord.co
#End of variable inputs
#Begin user creation
$objUser=$objOU.Create("user","CN="+$cn)
$objUser.Put("sAMAccountName",$sAMAccountName)
$objUser.Put('userPrincipalName',$userPrincipalName)
$objUser.Put("displayName",$displayName)
$objUser.Put('givenName',$givenName)
$objUser.Put("sn",$sn)
$objUser.Put("physicalDeliveryOfficeName",$physicalDeliveryOfficeName)
$objUser.Put("title",$title)
$objUser.Put('telephoneNumber',$telephone)
$objUser.Put("department",$department)
$objUser.Put('mail',$email)
$objUser.Put("l",$location)
$objUser.Put("street",$street)
$objUser.Put("postalCode",$postalCode)
$objUser.Put("mobile",$mobilephone)
$objUser.Put("co",$country)
$objUser.SetInfo()
#Setting the Password and enabling the account
$objUser.psbase.InvokeSet('AccountDisabled',$false)
#Change Password on first login
$objUser.PwdLastSet=0
$objUser.Setinfo()
$objUser.SetPassword("P@55w0rd")
}
Hi chrismerritt, thanks for jumping in to lend a hand. I'll give your code a try, but was hoping it could be a one-liner as this is to create users for AD accounts. We're quickly developing a series of scripts as a last-second requirement for a project we're on, and so I wanted to see if we could keep it as simple as possible. However, I'll cut and paste your code when I have a bit more time to see if it will do the trick.
I appreciate your help with this, and will let you know how it works out.
Sure thing, based on your code try this for mine:
Function Convert-WordsToProperCase
{
param
(
$Word
)
#Split the string in spaces
$WordSplit = ($word.Split(" "))
#Count the number of words in the string
$WordCount = $WordSplit.Count
#Loop through each word
for ($i = 0; $i -lt $WordCount; $i++)
{
#Get the current word
$CurrentWord = $WordSplit[$i]
#Set the first character in the word to UPPER case. The rest to LOWER case
$CurrentWordCharUpper = ($CurrentWord.SubString(0, 1)).ToUpper()
$CurrentWordCharLower = ($CurrentWord.SubString(1, ($CurrentWord.Length - 1))).ToLower()
#String the word together
$NewCurrentWord = $CurrentWordCharUpper + $CurrentWordCharLower
#Add the word to the concat string var
$WordConcat += $NewCurrentWord + " "
}
#Take the space off the end
$WordConcat = $WordConcat.SubString(0, $WordConcat.Length - 1)
#Return the word with caps changed accordingly
return $WordConcat
}
$objOU=[ADSI]"LDAP://OU=Staging,OU=Country,OU=LA,DC=mylab,DC=local"
$dataSource=import-csv "userlist.csv"
foreach($dataRecord in $datasource) {
$cn=$dataRecord.givenName + " " + $dataRecord.sn
$sAMAccountName=$dataRecord.givenName + "." + $dataRecord.sn
$givenName=$dataRecord.givenName
$sn=$dataRecord.sn
$sAMAccountName=$sAMAccountName.ToLower()
$displayname = Convert-WordsToProperCase ($sn + ", " + $givenName)
$userPrincipalName=$sAMAccountName + "@pumalab.local"
$physicalDeliveryOfficeName=$dataRecord.physicalDeliveryOfficeName
$title=$dataRecord.title
$telephone=$dataRecord.telephoneNumber
$department=$dataRecord.department
$email=$dataRecord.mail
$location=$dataRecord.l
$street=$dataRecord.street
$postalcode=$dataRecord.postalCode
$mobilephone=$dataRecord.mobile
$country=$dataRecord.co
#End of variable inputs
#Begin user creation
$objUser=$objOU.Create("user","CN="+$cn)
$objUser.Put("sAMAccountName",$sAMAccountName)
$objUser.Put('userPrincipalName',$userPrincipalName)
$objUser.Put("displayName",$displayName)
$objUser.Put('givenName',$givenName)
$objUser.Put("sn",$sn)
$objUser.Put("physicalDeliveryOfficeName",$physicalDeliveryOfficeName)
$objUser.Put("title",$title)
$objUser.Put('telephoneNumber',$telephone)
$objUser.Put("department",$department)
$objUser.Put('mail',$email)
$objUser.Put("l",$location)
$objUser.Put("street",$street)
$objUser.Put("postalCode",$postalCode)
$objUser.Put("mobile",$mobilephone)
$objUser.Put("co",$country)
$objUser.SetInfo()
#Setting the Password and enabling the account
$objUser.psbase.InvokeSet('AccountDisabled',$false)
#Change Password on first login
$objUser.PwdLastSet=0
$objUser.Setinfo()
$objUser.SetPassword("P@55w0rd")
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi KenMcF,
Thank you for reworking that! Sorry for my delay, as I've been crazy busy on this project. Was able to use your code and it worked like a charm! Again, sorry for my delay. I'm going to award you the points, as yours was a single string to do what I needed, which kept it simpler for me.
chrismerritt, I'm very grateful you jumped in to help as well. I was really looking for a single string solution so as to keep things as simple as possible for me as I learn scripting for admin automation. This is because I had to be ready to explain the functions in each of my scripts I'm developing, and I'm just not familiar enough with PowerShell to explain everything that was going on with the separate section above my original code.
Thank you both for jumping in and lending me a hand. It is very much appreciated.
Thank you for reworking that! Sorry for my delay, as I've been crazy busy on this project. Was able to use your code and it worked like a charm! Again, sorry for my delay. I'm going to award you the points, as yours was a single string to do what I needed, which kept it simpler for me.
chrismerritt, I'm very grateful you jumped in to help as well. I was really looking for a single string solution so as to keep things as simple as possible for me as I learn scripting for admin automation. This is because I had to be ready to explain the functions in each of my scripts I'm developing, and I'm just not familiar enough with PowerShell to explain everything that was going on with the separate section above my original code.
Thank you both for jumping in and lending me a hand. It is very much appreciated.
Open in new window