Updating UPN names in Active Directory

We are in the process of testing out Office 365, to see if it will be useful for us, so initially we are just going to use DirSync for some specific users, instead of setting up the complete ADFS solution. I have been extremely busy lately, so I decided to hire someone to come in and setup the DirSync and change the UPN’s for the users who are going to the cloud. Everything went fine, I gave him a list of OU’s which contained the users who needed the change, and he started opening each user going in and changing the UPN from the GUI.. But apparently I have gotten allergic to doing stuff in the GUI, so I ended writing a small script for him. Which after some procrastinating ended up with a GUI. It will load a tree view of the current directory (Requires AD cmdlets to be present on the system), then you can select each OU you want to change the containing users UPN name, it will also let you choose to recurse through multiple OU’s. Be aware this is version 1, so there are no “are you sure prompts”, proceed with caution.. I have exported the code from PowerShell studio with recovery info, so you can load the form, and work with it.

The treeview code is based on code from Thepip3r:

http://thepip3r.blogspot.dk/2011/06/powershell-guis-active-directory.html

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# Source File Information (DO NOT MODIFY)
# Source ID: c420d64b-72fd-475f-8ca7-939cd9a6285e
# Source File: C:\Users\ctn\Documents\SAPIEN\PowerShell Studio 2012\Files\OU Picker.pff
#------------------------------------------------------------------------
#region File Recovery Data (DO NOT MODIFY)
<#RecoveryData:
JioAAB+LCAAAAAAABAC9Wttzok4Wft+q/R8sX/Yh2eA9pspY1SAmZqPRqLm9pFpsGUYEfw0EzV+/
p7nIXSEzyUyV5hy6z+U7H6ebls4jkfQPQvc9bOLuv/9VKnUeqCIrGlb7ikpGeEO6D/PSWJHWhF5s
V6sOl7juzGLSE6GGomvd6kWtw4UVrt3FbyKZJXO/Jdfl6d4wyebiWdGWum1c9HW6cT/PS2mXzkue
qevGRYX9Py8JlmpalFxrxDIpVs9LY2uhKtL/yH6mr4l2vbi8xE2p2ape1Ruk0r4qlzQI9rq8AnvC
L6zJZD4esfiNckn6pahLCpPKgq6ZVFeNshMzRD2m+pZQc+/NFlSFaOZU+STlbrN1dV5qNSodzh+U
MYl5KXeTjk9OnJGdWe66k0owy1GnTBM/IChvzr2Ol2nO3tmFDucM9eedLonwi0hrXt99e1kk5mih
74CPFjXIw/xoWZJQ3esSNiGScrfRbJ2XoDYJmLIKk3SdY6pLgWqlcV6qNXKMn+HFQFuSXbmbJzC3
8F5ApYf5f/KENDfIk2JYWJ2ae5XwWFoLuqrTcndGLZI2P0wbp9Bk6XJmmQbKe3RIjEqcy6V0ZqHx
QBzN4G/jAqKQKTEMHtMH6Dsq3gNFdJvQ6S+iqqWpaS0VPeBV/QKayVUeYmmWqvpk2npOFpjqrpPq
V9lUrQGZKnlqNsQ7ZWNtGClyUy8l0NzcazYAlVo9z3jGh3J3iOk/FkllQiZZ2znJ6tWy3GVdRtFk
4CzUkloSq1kOG0BcZcGC7GPVSE44Tq+0xnWPF0T99q6lMi8jYkOTdZfIL9MM+kiz3sxNnITjAi0r
L20CFlzmblkQkrNQZQRVvI68ZZq69u2FXDhuDivmHy4+V/kXn7jj3HW8ahUuYyt3GYMtxw+sPFCf
dRKId0dfZJVJ3b/om4X+I/sX5gjWy4ILTQ/Enm5rX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zC4y8nOe6Xs0ox/p/Jvv7FxzuRXxAPn8q7WDzp
eK3D/pcp64Ec48MJe5OMPMWwn7sMvCXQv6CkfzFjvFiwnlJkfXTvIyb30+/bY/encap/xOoWrXMG
P0cF1+lhxn2Tlf/wiP003IcF8R1m7BuGGbgP89xfdbvY+pi1fmWtm9Uj63u871U9fdo+IWvdP2Y/
vn8I7pc4r+TI/iiGV2IfkxVHkX2Anbr+yhnrYCo/U/21CuJxWTDuHYTo7rvP2L57nL3v5nnYt8vw
3Yf9NNcQYXCb55DIoZXNj+2Z933bRrN2j7Nve0xG9i3YG0bliS8D/vaYQ9KQ4cJkvs3iWjH8bm0m
n02icvz6KRnma659xx/ftO85ZASyzWQJsbjO0NzTf8A4JPrxcohIAufm5eQb5N3gV0HeZ+ilLcbH
2UjUmH24riHOkXtIGsA88MOx/G9Z/i4eROJt1x/I4pjF12PyBOzZN6uoHL9+Sob5zL/rD+R7J890
2YmX1Uk44MTydXCK1DmSr2BfX3c476E39aE/5SSFYs3YYko083CEdtAcPTJAhkE28EBPDN+0p9l3
vQOCIdbgqXwDhi6QZeruE3z4p+ScBwX16mJVbzdbeFlvNQj7Ue7gKe55Y0g6VZXFXziOOOLFze8n
fPz1Q5bTHtmLRz/jiGJb0eSv+KrUV83V5apaXTYruI5P+3rZqD+Tk0LhBtHpfkrohyKRL5WscHaC
TsmPpOdlBV0BUvuG3A6i31U6YyytoYlQP6IbohEwHDoZdgccXnzzul6a1lMOJO8NuMjQqBZal7Ii
hilQ4nQt9jtGQncYLVgGtDf/esh21oWxik12Vtv9Lzse94XgWNfSpjPEzra9v4KJhzdz/Ff52M8x
CaXf/KNodabs3SEFev/hTDg6IfHaoFPV5KuD/kKytCQzMTimj4+PFypN2yOGRJVtFGQuVSvomy3W
9mHM4xpB3+6pIv+KFCapG2gmoRpWYwGmq1PfuXQnZF+CyDaRXzO5qKbDRd7P5ELcByaE3wv9P2IG
PnImKgAA#>
#endregion
#========================================================================
# Code Generated By: SAPIEN Technologies, Inc., PowerShell Studio 2012 v3.1.29
# Generated On: 22-02-2014 20:16
# Generated By: CTNADM
#========================================================================
#----------------------------------------------
#region Application Functions
#----------------------------------------------

function OnApplicationLoad {
	#Note: This function is not called in Projects
	#Note: This function runs before the form is created
	#Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path
	#Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList)
	#Important: Form controls cannot be accessed in this function
	#TODO: Add snapins and custom code to validate the application load
	#Import-Module Pipeworks
	#Import-Module Pscx
	Import-Module Pipeworks
	
	
	if (-not (Get-Module ActiveDirectory)){            
 		 Import-Module ActiveDirectory            
	}            
   
	#TODO: Place custom script here
	$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()            
	$Script:strDomainDN = ($domain.GetDirectoryEntry()).distinguishedName 
	$Script:upnDN = "cn=Partitions,cn=Configuration,$Script:strDomainDN" 
	
	return $true #return true for success or false for failure
}

function OnApplicationExit {
	#Note: This function is not called in Projects
	#Note: This function runs after the form is closed
	#TODO: Add custom code to clean up and unload snapins when the application exits
	
	$script:ExitCode = 0 #Set the exit code for the Packager
}

#endregion Application Functions

#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-OU_Picker_pff {

	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
	[void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
	[void][reflection.assembly]::Load("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
	#endregion Import Assemblies

	#----------------------------------------------
	#region Define SAPIEN Types
	#----------------------------------------------
	try{
		$local:type = [ProgressBarOverlay]
	}
	catch
	{
		Add-Type -ReferencedAssemblies ('System.Windows.Forms', 'System.Drawing') -TypeDefinition  @" 
		using System;
		using System.Windows.Forms;
		using System.Drawing;
        namespace SAPIENTypes
        {
		    public class ProgressBarOverlay : System.Windows.Forms.ProgressBar
	        {
	            protected override void WndProc(ref Message m)
	            { 
	                base.WndProc(ref m);
	                if (m.Msg == 0x000F)// WM_PAINT
	                {
	                    if (Style != System.Windows.Forms.ProgressBarStyle.Marquee || !string.IsNullOrEmpty(this.Text))
                        {
                            using (Graphics g = this.CreateGraphics())
                            {
                                using (StringFormat stringFormat = new StringFormat(StringFormatFlags.NoWrap))
                                {
                                    stringFormat.Alignment = StringAlignment.Center;
                                    stringFormat.LineAlignment = StringAlignment.Center;
                                    if (!string.IsNullOrEmpty(this.Text))
                                        g.DrawString(this.Text, this.Font, Brushes.Black, this.ClientRectangle, stringFormat);
                                    else
                                    {
                                        int percent = (int)(((double)Value / (double)Maximum) * 100);
                                        g.DrawString(percent.ToString() + "%", this.Font, Brushes.Black, this.ClientRectangle, stringFormat);
                                    }
                                }
                            }
                        }
	                }
	            }
              
                public string TextOverlay
                {
                    get
                    {
                        return base.Text;
                    }
                    set
                    {
                        base.Text = value;
                        Invalidate();
                    }
                }
	        }
        }
"@ | Out-Null
	}
	#endregion Define SAPIEN Types

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$formChangeUPNNames = New-Object 'System.Windows.Forms.Form'
	$checkboxRecurseOUs = New-Object 'System.Windows.Forms.CheckBox'
	$progressbaroverlay1 = New-Object 'SAPIENTypes.ProgressBarOverlay'
	$labelNewUPNPicker = New-Object 'System.Windows.Forms.Label'
	$buttonChangeUPNs = New-Object 'System.Windows.Forms.Button'
	$combobox1 = New-Object 'System.Windows.Forms.ComboBox'
	$buttonListOUs = New-Object 'System.Windows.Forms.Button'
	$treeview1 = New-Object 'System.Windows.Forms.TreeView'
	$errorprovider1 = New-Object 'System.Windows.Forms.ErrorProvider'
	$imagelistButtonBusyAnimation = New-Object 'System.Windows.Forms.ImageList'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	
	
	
	
	
	
	
	$formChangeUPNNames_Load={
		#TODO: Initialize Form Controls here
		Load-ComboBox $combobox1 $(Get-ADObject -Identity $Script:upnDN  -Properties upnsuffixes | select -ExpandProperty upnsuffixes)
		$errorprovider1.SetError($combobox1, "Choose UPN")
		
	}
	
	
	
	#region Control Helper Functions
	function Load-ListBox 
	{
	<#
		.SYNOPSIS
			This functions helps you load items into a ListBox or CheckedListBox.
	
		.DESCRIPTION
			Use this function to dynamically load items into the ListBox control.
	
		.PARAMETER  ListBox
			The ListBox control you want to add items to.
	
		.PARAMETER  Items
			The object or objects you wish to load into the ListBox's Items collection.
	
		.PARAMETER  DisplayMember
			Indicates the property to display for the items in this control.
		
		.PARAMETER  Append
			Adds the item(s) to the ListBox without clearing the Items collection.
		
		.EXAMPLE
			Load-ListBox $ListBox1 "Red", "White", "Blue"
		
		.EXAMPLE
			Load-ListBox $listBox1 "Red" -Append
			Load-ListBox $listBox1 "White" -Append
			Load-ListBox $listBox1 "Blue" -Append
		
		.EXAMPLE
			Load-ListBox $listBox1 (Get-Process) "ProcessName"
	#>
		Param (
			[ValidateNotNull()]
			[Parameter(Mandatory=$true)]
			[System.Windows.Forms.ListBox]$ListBox,
			[ValidateNotNull()]
			[Parameter(Mandatory=$true)]
			$Items,
		    [Parameter(Mandatory=$false)]
			[string]$DisplayMember,
			[switch]$Append
		)
		
		if(-not $Append)
		{
			$listBox.Items.Clear()	
		}
		
		if($Items -is [System.Windows.Forms.ListBox+ObjectCollection])
		{
			$listBox.Items.AddRange($Items)
		}
		elseif ($Items -is [Array])
		{
			$listBox.BeginUpdate()
			foreach($obj in $Items)
			{
				$listBox.Items.Add($obj)
			}
			$listBox.EndUpdate()
		}
		else
		{
			$listBox.Items.Add($Items)	
		}
	
		$listBox.DisplayMember = $DisplayMember	
	}
	
	function Load-ComboBox 
	{
	<#
		.SYNOPSIS
			This functions helps you load items into a ComboBox.
	
		.DESCRIPTION
			Use this function to dynamically load items into the ComboBox control.
	
		.PARAMETER  ComboBox
			The ComboBox control you want to add items to.
	
		.PARAMETER  Items
			The object or objects you wish to load into the ComboBox's Items collection.
	
		.PARAMETER  DisplayMember
			Indicates the property to display for the items in this control.
		
		.PARAMETER  Append
			Adds the item(s) to the ComboBox without clearing the Items collection.
		
		.EXAMPLE
			Load-ComboBox $combobox1 "Red", "White", "Blue"
		
		.EXAMPLE
			Load-ComboBox $combobox1 "Red" -Append
			Load-ComboBox $combobox1 "White" -Append
			Load-ComboBox $combobox1 "Blue" -Append
		
		.EXAMPLE
			Load-ComboBox $combobox1 (Get-Process) "ProcessName"
	#>
		Param (
			[ValidateNotNull()]
			[Parameter(Mandatory=$true)]
			[System.Windows.Forms.ComboBox]$ComboBox,
			[ValidateNotNull()]
			[Parameter(Mandatory=$true)]
			$Items,
		    [Parameter(Mandatory=$false)]
			[string]$DisplayMember,
			[switch]$Append
		)
		
		if(-not $Append)
		{
			$ComboBox.Items.Clear()	
		}
		
		if($Items -is [Object[]])
		{
			$ComboBox.Items.AddRange($Items)
		}
		elseif ($Items -is [Array])
		{
			$ComboBox.BeginUpdate()
			foreach($obj in $Items)
			{
				$ComboBox.Items.Add($obj)	
			}
			$ComboBox.EndUpdate()
		}
		else
		{
			$ComboBox.Items.Add($Items)	
		}
	
		$ComboBox.DisplayMember = $DisplayMember	
	}
	
	function Get-CheckedNodes 
	{
	<#
		.SYNOPSIS
			This function collects a list of checked nodes in a TreeView
	
		.DESCRIPTION
			This function collects a list of checked nodes in a TreeView
	
		.PARAMETER  $NodeCollection
			The collection of nodes to search
	
		.PARAMETER  $CheckedNodes
			The ArrayList that will contain the all the checked items
		
		.EXAMPLE
			$CheckedNodes = New-Object System.Collections.ArrayList
			Get-CheckedNodes $treeview1.Nodes $CheckedNodes
			foreach($node in $CheckedNodes)
			{	
				Write-Host $node.Text
			}
	#>
		param(
		[ValidateNotNull()]
		[System.Windows.Forms.TreeNodeCollection] $NodeCollection,
		[ValidateNotNull()]
		[System.Collections.ArrayList]$CheckedNodes)
		
		foreach($Node in $NodeCollection)
		{
			if($Node.Checked)
			{
				[void]$CheckedNodes.Add($Node)
			}
			Get-CheckedNodes $Node.Nodes $CheckedNodes
		}
	}
	
	#endregion
	
	
	
	#Add-Node, get-NextLevel and Build Treeview code is taken from http://thepip3r.blogspot.nl/2011/06/powershell-guis-active-directory.html
	function Add-Node { 
	        param ( 
	            $selectedNode, 
	            $name
	        ) 
	        $newNode = new-object System.Windows.Forms.TreeNode  
	        $newNode.Name = $name 
	        $newNode.Text = $name 
	        $selectedNode.Nodes.Add($newNode) | Out-Null 
	        return $newNode 
	} 
	
	function Get-NextLevel {
	    param (
	        $selectedNode,
	        [String]$dn
	   )
	   [System.Windows.Forms.Application]::DoEvents()
	    $OUs = Get-ADObject -Filter 'ObjectClass -eq "organizationalUnit" -or ObjectClass -eq "container"' -SearchScope OneLevel -SearchBase $dn
	
	    If ($OUs -eq $null) {
	        $node = Add-Node $selectedNode $dn
	    } Else {
	        $node = Add-Node $selectedNode $dn
	        
	        $OUs | % {
	            Get-NextLevel $node $_.distinguishedName
	        }
	    }
	}
	 
	function Build-TreeView { 
	    if ($treeNodes)  
	    {  
	          $treeview1.Nodes.remove($treeNodes) 
	        $formChangeUPNNames.Refresh() 
	    } 
	    
	    $treeNodes = New-Object System.Windows.Forms.TreeNode 
	    $treeNodes.text = "Active Directory Root" 
	    $treeNodes.Name = "Active Directory Root" 
	    $treeNodes.Tag = "root" 
	    $treeView1.Nodes.Add($treeNodes) | Out-Null 
	     
	    #$treeView1.add_AfterSelect({ 
	    #    $textbox1.Text = $this.SelectedNode.Name
	    #}) 
	     
	    #Generate Module nodes 
	    $OUs = Get-NextLevel $treeNodes $Script:strDomainDN
	    
	    $treeNodes.Expand() 
	} 
	$buttonListOUs_Click={
		
		
		$progressbaroverlay1.Visible = $true
		
		$progressbaroverlay1.Style = 'Marquee'
		[System.Windows.Forms.Application]::DoEvents()
		Build-TreeView
		
		$progressbaroverlay1.Visible = $false
	  
		
		#TODO: Place custom script here
		
	}
	
	$button2_Click={
		
			
		Load-ComboBox $combobox1 $(Get-ADObject -Identity $Script:upnDN  -Properties upnsuffixes | select -ExpandProperty upnsuffixes)
		
	}
	
	
	$buttonChangeUPNs_Click={
		#TODO: Place custom script here
		$CheckedNodes = New-Object System.Collections.ArrayList
		Get-CheckedNodes $treeview1.Nodes $CheckedNodes
			foreach($node in $CheckedNodes)
			{	
				
				If ($checkboxRecurseOUs.Checked) {
					Get-ADUser -Filter * -SearchBase "$($node.text)"  |
						ForEach-Object {
	    					$UPN = $_.UserPrincipalName -Replace '(?im)@.+$', "@$($combobox1.SelectedItem.ToString())"
							Write-Host $UPN
							Write-Host $_.UserPrincipalName
	    					Set-ADUser $_ -UserPrincipalName $UPN 
						}				
					$combobox1.SelectedItem
					Write-Host $node.Text
				}
			
				Else {
					Get-ADUser -Filter * -SearchBase "$($node.text)" -SearchScope OneLevel |
						ForEach-Object {
	    					$UPN = $_.UserPrincipalName -Replace '(?im)@.+$', "@$($combobox1.SelectedItem.ToString())"
							Write-Host $UPN
							Write-Host $_.UserPrincipalName
	    					Set-ADUser $_ -UserPrincipalName $UPN 
						}				
					$combobox1.SelectedItem
					Write-Host $node.Text
				}
			}
	}
	
	
	$combobox1_Validating=[System.ComponentModel.CancelEventHandler]{
	#Event Argument: $_ = [System.ComponentModel.CancelEventArgs]
		#TODO: Place custom script here
		
			
	}
	
	$combobox1_Leave={
		#TODO: Place custom script here
		If ((-not $combobox1.SelectedItem) -or ($combobox1.SelectedItem.ToString() -like "")) {
				$errorprovider1.SetError($combobox1,"Select a value from the list")	
			}
			Else {
				$errorprovider1.SetError($combobox1,"")
				}
			
	}
	
	$combobox1_DropDownClosed={
		#TODO: Place custom script here
			If ((-not $combobox1.SelectedItem) -or ($combobox1.SelectedItem.ToString() -like "")) {
				$errorprovider1.SetError($combobox1,"Select a value from the list")	
			}
			Else {
				$errorprovider1.SetError($combobox1,"")
				}
	}
	
	
	
	
	$combobox1_TextChanged={
		#TODO: Place custom script here
		If ((-not $combobox1.SelectedItem) -or ($combobox1.SelectedItem.ToString() -like "")) {
				$errorprovider1.SetError($combobox1,"Select a value from the list")	
			}
			Else {
				$errorprovider1.SetError($combobox1,"")
				}
	}
	
	$checkboxRecurseOUs_CheckedChanged={
		#TODO: Place custom script here
		
	}
	
	# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load=
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$formChangeUPNNames.WindowState = $InitialFormWindowState
	}
	
	$Form_Cleanup_FormClosed=
	{
		#Remove all event handlers from the controls
		try
		{
			$checkboxRecurseOUs.remove_CheckedChanged($checkboxRecurseOUs_CheckedChanged)
			$buttonChangeUPNs.remove_Click($buttonChangeUPNs_Click)
			$combobox1.remove_TextChanged($combobox1_TextChanged)
			$combobox1.remove_Leave($combobox1_Leave)
			$combobox1.remove_Validating($combobox1_Validating)
			$buttonListOUs.remove_Click($buttonListOUs_Click)
			$formChangeUPNNames.remove_Load($formChangeUPNNames_Load)
			$formChangeUPNNames.remove_Load($Form_StateCorrection_Load)
			$formChangeUPNNames.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch [Exception]
		{ }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	#
	# formChangeUPNNames
	#
	$formChangeUPNNames.Controls.Add($checkboxRecurseOUs)
	$formChangeUPNNames.Controls.Add($progressbaroverlay1)
	$formChangeUPNNames.Controls.Add($labelNewUPNPicker)
	$formChangeUPNNames.Controls.Add($buttonChangeUPNs)
	$formChangeUPNNames.Controls.Add($combobox1)
	$formChangeUPNNames.Controls.Add($buttonListOUs)
	$formChangeUPNNames.Controls.Add($treeview1)
	$formChangeUPNNames.ClientSize = '569, 640'
	$formChangeUPNNames.Name = "formChangeUPNNames"
	$formChangeUPNNames.Text = "Change UPN names"
	$formChangeUPNNames.add_Load($formChangeUPNNames_Load)
	#
	# checkboxRecurseOUs
	#
	$checkboxRecurseOUs.Location = '456, 569'
	$checkboxRecurseOUs.Name = "checkboxRecurseOUs"
	$checkboxRecurseOUs.Size = '104, 24'
	$checkboxRecurseOUs.TabIndex = 9
	$checkboxRecurseOUs.Text = "Recurse OU's"
	$checkboxRecurseOUs.UseVisualStyleBackColor = $True
	$checkboxRecurseOUs.add_CheckedChanged($checkboxRecurseOUs_CheckedChanged)
	#
	# progressbaroverlay1
	#
	$progressbaroverlay1.Location = '12, 509'
	$progressbaroverlay1.Maximum = 10
	$progressbaroverlay1.Name = "progressbaroverlay1"
	$progressbaroverlay1.Size = '540, 23'
	$progressbaroverlay1.Style = 'Marquee'
	$progressbaroverlay1.TabIndex = 8
	$progressbaroverlay1.TextOverlay = "Loading OU Structure"
	$progressbaroverlay1.Visible = $False
	#
	# labelNewUPNPicker
	#
	$labelNewUPNPicker.Location = '14, 535'
	$labelNewUPNPicker.Name = "labelNewUPNPicker"
	$labelNewUPNPicker.Size = '100, 23'
	$labelNewUPNPicker.TabIndex = 7
	$labelNewUPNPicker.Text = "New UPN Picker"
	#
	# buttonChangeUPNs
	#
	$buttonChangeUPNs.Location = '456, 599'
	$buttonChangeUPNs.Name = "buttonChangeUPNs"
	$buttonChangeUPNs.Size = '96, 23'
	$buttonChangeUPNs.TabIndex = 6
	$buttonChangeUPNs.Text = "Change UPN's"
	$buttonChangeUPNs.UseVisualStyleBackColor = $True
	$buttonChangeUPNs.add_Click($buttonChangeUPNs_Click)
	#
	# combobox1
	#
	$combobox1.DropDownStyle = 'DropDownList'
	$combobox1.FormattingEnabled = $True
	$combobox1.Location = '14, 559'
	$combobox1.Name = "combobox1"
	$combobox1.Size = '165, 21'
	$combobox1.TabIndex = 3
	$combobox1.add_TextChanged($combobox1_TextChanged)
	$combobox1.add_Leave($combobox1_Leave)
	$combobox1.add_Validating($combobox1_Validating)
	#
	# buttonListOUs
	#
	$buttonListOUs.Location = '14, 599'
	$buttonListOUs.Name = "buttonListOUs"
	$buttonListOUs.Size = '84, 23'
	$buttonListOUs.TabIndex = 1
	$buttonListOUs.Text = "List OU's"
	$buttonListOUs.UseVisualStyleBackColor = $True
	$buttonListOUs.add_Click($buttonListOUs_Click)
	#
	# treeview1
	#
	$treeview1.CheckBoxes = $True
	$treeview1.Location = '12, 27'
	$treeview1.Name = "treeview1"
	$treeview1.Size = '540, 478'
	$treeview1.TabIndex = 0
	#
	# errorprovider1
	#
	$errorprovider1.ContainerControl = $formChangeUPNNames
	#
	# imagelistButtonBusyAnimation
	#
	$Formatter_binaryFomatter = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
	#region Binary Data
	$System_IO_MemoryStream = New-Object System.IO.MemoryStream (,[byte[]][System.Convert]::FromBase64String('
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAu
MC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAA
ACZTeXN0ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkD
AAA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HDAfwBAwHA
AQMD/wHDAf8DwwP/AcMB/wPDAf8B8AH/AfAB/wHwAf8B+QH/AfAB/wHwAf8B8AH/AfAB/wHwAf8B
8AH/AfAB/wHwAf8B8AH/AfAB/wHwAf8B+QHnAcMB/wHDAf8B5wL/AsMB/wHDAf8BwwL/AcABAwH+
AUMB/wHDAv8B5AEDAfwBAwH/AecC/wH8AT8B/AE/BP8B/AE/Af4BfwT/AfwBPwH+AX8E/wH8AT8B
/AE/BP8BwAEnAcABPwHnA/8BwAEDAcIBfwHDA/8DwwH/AcMD/wHDAecBwwH/AecD/wEPAf8BDwH/
AQ8B/wGfAf8BDwH/AQ8B/wEPAf8BDwH/AQ8B/wEPAf8BDwH/AQ8B/wEPAf8BDwH/AQ8B/wGfA/8B
wwH/AcMB/wLDAv8BwwH/AcMB/wLDAv8BwwH/AcABPwHAAQMC/wHDAf8BwAE/AcABAwT/AfwBPwH8
AT8E/wH8AT8B/AE/Cw=='))
	#endregion
	$imagelistButtonBusyAnimation.ImageStream = $Formatter_binaryFomatter.Deserialize($System_IO_MemoryStream)
	$Formatter_binaryFomatter = $null
	$System_IO_MemoryStream = $null
	$imagelistButtonBusyAnimation.TransparentColor = 'Transparent'
	#endregion Generated Form Code

	#----------------------------------------------
#Save the initial state of the form $InitialFormWindowState = $formChangeUPNNames.WindowState #Init the OnLoad event to correct the initial state of the form $formChangeUPNNames.add\_Load($Form\_StateCorrection\_Load) #Clean up the control events $formChangeUPNNames.add\_FormClosed($Form\_Cleanup\_FormClosed) #Show the Form return $formChangeUPNNames.ShowDialog() } #End Function #Call OnApplicationLoad to initialize if((OnApplicationLoad) -eq $true) { #Call the form Call-OU\_Picker\_pff | Out-Null #Perform cleanup OnApplicationExit }
comments powered by Disqus