{"id":3042,"date":"2013-08-14T00:01:00","date_gmt":"2013-08-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/08\/14\/use-powershell-to-change-sign-in-script-and-profile-path\/"},"modified":"2013-08-14T00:01:00","modified_gmt":"2013-08-14T00:01:00","slug":"use-powershell-to-change-sign-in-script-and-profile-path","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-change-sign-in-script-and-profile-path\/","title":{"rendered":"Use PowerShell to Change Sign-in Script and Profile Path"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to modify the sign-in script and profile path in Active Directory.<\/p>\n<p>Hey, Scripting Guy! We are in the middle of an Active Directory migration (primarily moving our client computers from Windows&nbsp;XP to Windows&nbsp;8). We are also consolidating our file servers and our profile servers. We have multiple sites, and in the past, each site had a one or more domain controllers, multiple file and print servers, and other stuff as needed.<\/p>\n<p>Now, we are collapsing that infrastructure into a single server running Hyper-V. Needless to say, our profiles will be moving to different servers, and we will also be changing our sign-in scripts. So I need an easy way to modify these settings for our users. The new servers will be based on the user&rsquo;s city locations. Can you help?<\/p>\n<p>&mdash;RA<\/p>\n<p>Hello RA,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Things have been busy around the Scripting House. I got up early to check the <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a> email and to write a couple of proposals for <a href=\"http:\/\/powershellsaturday.com\/005\/\" target=\"_blank\">Windows PowerShell Saturday in Atlanta<\/a>. According to Mark, I will be making two presentations&mdash;one for the beginner track and one for the advanced track. In addition, I have been working on my presentation that I will be conducting remotely for <a href=\"http:\/\/powershellsaturday.com\/006\/\" target=\"_blank\">Windows PowerShell Saturday in Singapore<\/a>.<\/p>\n<h2>Find the attribute names<\/h2>\n<p>The first thing we need to do is to find the ADSI attribute names for the profile path and for the sign-in script. I open up one of the user profiles and type some bogus information so that I can find the attributes in ADSI Edit. Here is the page from Active Directory Users and Computers:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1512.HSG-8-14-13-01.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1512.HSG-8-14-13-01.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>Now I navigate to the same user object in ADSI Edit and look up the ADSI property names. The names make sense: ProfilePath and ScriptPath. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0804.HSG-8-14-13-02.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0804.HSG-8-14-13-02.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<h2>Get the information from AD&nbsp;DS<\/h2>\n<p>Now I need to retrieve the information from Active Directory Domain Services (AD&nbsp;DS). I could do all this from inside the Windows PowerShell console, but I decided to use the Windows PowerShell ISE instead. It has better intellisense, and for something like this, it makes things a bit more readable. I decide to use a couple of variables to hold the organizational unit (OU) and the properties that I need to retrieve. I then use <strong>Get-ADUser<\/strong> to retrieve the information. Here is this portion of the script:<\/p>\n<p style=\"padding-left: 30px\"><strong>Import-Module ActiveDirectory<\/strong><\/p>\n<p style=\"padding-left: 30px\">$ou = &#8220;OU=Testou,Dc=Iammred,Dc=Net&#8221;<\/p>\n<p style=\"padding-left: 30px\">$properties = &#8220;ProfilePath&#8221;,&#8221;ScriptPath&#8221;, &#8220;l&#8221;<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -Filter * -SearchBase $ou -Properties $properties<\/p>\n<p>I can highlight only this section of the script to test it. After I see that it works, I pipe the returned information to the <strong>Foreach-Object<\/strong> cmdlet. The hardest part of the script is to create the profile path and the script path. I decide to use parameter substitution and the <strong>Format<\/strong> operator to do this because, for me anyway, it is easier to read.<\/p>\n<p>I build the profile path based on the city name. I then add <strong>Storage1<\/strong> (which is the name of the storage server) and <strong>Profiles<\/strong> (which is the name of the folder that holds the profiles). Next, I use the user&rsquo;s <strong>SamAccountName<\/strong> attribute. Here is the string:<\/p>\n<p style=\"padding-left: 30px\">$ProfilePath = &#8220;{0}\\storage1\\profiles\\{1}&#8221; -f $_.l, $_.SamAccountName<\/p>\n<p>Now, to create<strong> <\/strong>the script path. To do that, I again use the city name. I also store the scripts in <strong>Storage1<\/strong>, and I place them in a folder named <strong>Scripts<\/strong>. The sign-in script is based on the city name and the word <strong>LogonScript<\/strong>. Therefore, I am only substituting a single word: the city name, which is the<strong> l<\/strong> attribute. Here is the string I use for this:<\/p>\n<p style=\"padding-left: 30px\">$ScriptPath = &#8220;{0}\\storage1\\scripts\\{0}_logonScript.ps1&#8221; -f $_.l<\/p>\n<p>The rest is really easy. All I need to do is to use the <strong>Set-ADUser<\/strong> cmdlet to plug in the values. Here is that command:<\/p>\n<p style=\"padding-left: 30px\">Set-ADUser $_.samaccountname -ProfilePath $ProfilePath -ScriptPath $ScriptPath<\/p>\n<p>The complete script is shown here:<\/p>\n<p style=\"padding-left: 30px\"><strong>Import-Module ActiveDirectory<\/strong><\/p>\n<p style=\"padding-left: 30px\">$ou = &#8220;OU=Testou,Dc=Iammred,Dc=Net&#8221;<\/p>\n<p style=\"padding-left: 30px\">$properties = &#8220;ProfilePath&#8221;,&#8221;ScriptPath&#8221;, &#8220;l&#8221;<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -Filter * -SearchBase $ou -Properties $properties |<\/p>\n<p style=\"padding-left: 30px\">ForEach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$ProfilePath = &#8220;{0}\\storage1\\profiles\\{1}&#8221; -f $_.l, $_.SamAccountName<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$ScriptPath = &#8220;{0}\\storage1\\scripts\\{0}_logonScript.ps1&#8221; -f $_.l<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Set-ADUser $_.samaccountname -ProfilePath $ProfilePath -ScriptPath $ScriptPath<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>When I run the script, nothing returns. But that is what I want (I really do not want a whole bunch of errors). Here is the ISE and the blank output from running the script:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6014.HSG-8-14-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6014.HSG-8-14-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>I check Active Directory Users and computers to ensure that everything worked as planned. It is fine, as shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6663.HSG-8-14-13-04.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6663.HSG-8-14-13-04.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>RA, that is all there is to using Windows PowerShell to create values for the sign-in script and the profile path. Active Directory Week will continue tomorrow when I will talk about logging an attribute change.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to modify the sign-in script and profile path in Active Directory. Hey, Scripting Guy! We are in the middle of an Active Directory migration (primarily moving our client computers from Windows&nbsp;XP to Windows&nbsp;8). We are also consolidating our file servers and our profile servers. [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,3,20,45],"class_list":["post-3042","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-user-accounts","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to modify the sign-in script and profile path in Active Directory. Hey, Scripting Guy! We are in the middle of an Active Directory migration (primarily moving our client computers from Windows&nbsp;XP to Windows&nbsp;8). We are also consolidating our file servers and our profile servers. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3042"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3042\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}