Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How would I change the case of a string to a proper case?

Posted on 2011-09-30
8
Medium Priority
?
1,123 Views
Last Modified: 2012-05-12
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:

$displayName=$sn + ", " + $givenName

Open in new window


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!
0
Comment
Question by:HelpfulAdvisor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 27

Expert Comment

by:KenMcF
ID: 36894956
Try something like this


$displayname = $displayname = "$((Get-Culture).textinfo.totitlecase($sn)), $((Get-Culture).textinfo.totitlecase($givenname))"

Open in new window

0
 

Author Comment

by:HelpfulAdvisor
ID: 36895266
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.

0
 
LVL 27

Expert Comment

by:KenMcF
ID: 36895312
Can you post your full code


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))"

Open in new window

0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 9

Expert Comment

by:chrismerritt
ID: 36896586
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:

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
}

Open in new window


After the function code you can call the Function easily:

Convert-WordsToProperCase "SOME words HERE"

Open in new window


Result:

Some Words Here

In your case, with my function at the top of your script you call this:

$displayname = Convert-WordsToProperCase ($sn + ", " + $givenName)

Open in new window


Hope this helps?
0
 

Author Comment

by:HelpfulAdvisor
ID: 36903959
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

$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")
}

Open in new window


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.
0
 
LVL 9

Expert Comment

by:chrismerritt
ID: 36904252
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")
}

Open in new window

0
 
LVL 27

Accepted Solution

by:
KenMcF earned 500 total points
ID: 36904323
Try this, I think you want the CN to be the same case as displayname


$objOU=[ADSI]"LDAP://OU=LA,DC=devlab,DC=local"
$dataSource=import-csv "c:\temp\userlist.csv"
foreach($dataRecord in $datasource) {
$givenName=(Get-Culture).textinfo.totitlecase($dataRecord.givenName)
$sn=(Get-Culture).textinfo.totitlecase($dataRecord.sn)
$cn="$givenName $sn"
$sAMAccountName="$givenName.$sn"
$sAMAccountName=$sAMAccountName.ToLower()
$Displayname = "$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")
}

Open in new window

0
 

Author Comment

by:HelpfulAdvisor
ID: 36926146
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.
0

Featured Post

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question