Solved

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

Posted on 2011-09-30
8
967 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 125 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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