MSEndpointMgr
Home Β» Microsoft Endpoint Manager Β» ConfigMgr Β» Operating System Deployment Β» Prompt for Computer Name during OSD with PowerShell

Prompt for Computer Name during OSD with PowerShell

Instead of automating the way a new computer is being named during OSD with ConfigMgr 2012 SP1, you might want to be prompted instead and have to enter the name manually. There are numerous ways of doing this and most of them are doing it with a VB script. Since WinPE 4.0 we now have support for running PowerShell scripts. And since my skills in VB isn’t that great, I’m turning to PowerShell and Windows Forms in order to create a prompt asking me to enter the computer name.

The script

Save this script as OSDComputerNamePrompt.ps1 and store it in your Content Library location. In my lab environment the path is \\CM01\ContentLibrary$\OSD\Scripts\OSDComputerName.
As for naming your computers correctly, I’ve added some error handling in the script so that it accepts maximum 15 character. It will also remove any invalid characters that are not supported for a NetBIOS name.
Dave Green has created a modified version of this script, that allows you to hit Enter instead of clicking on the OK button. His script can be found here, thanks Dave for contributing!

function Load-Form {
    $Form.Controls.Add($TBComputerName)
    $Form.Controls.Add($GBComputerName)
    $Form.Controls.Add($ButtonOK)
    $Form.Add_Shown({$Form.Activate()})
    [void] $Form.ShowDialog()
}
function Set-OSDComputerName {
    $ErrorProvider.Clear()
    if ($TBComputerName.Text.Length -eq 0) {
        $ErrorProvider.SetError($GBComputerName, "Please enter a computer name")
    }
    else {
        if ($TBComputerName.Text.Length -gt 15) {
            $ErrorProvider.SetError($GBComputerName, "Computer name cannot be more than 15 characters")
        }
        else {
            $OSDComputerName = $TBComputerName.Text.Replace("[","").Replace("]","").Replace(":","").Replace(";","").Replace("|","").Replace("=","").Replace("+","").Replace("*","").Replace("?","").Replace("<","").Replace(">","").Replace("/","").Replace("\","").Replace(",","")
            $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
            $TSEnv.Value("OSDComputerName") = "$($OSDComputerName)"
            $Form.Close()
        }
    }
}
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Global:ErrorProvider = New-Object System.Windows.Forms.ErrorProvider
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(285,140)
$Form.MinimumSize = New-Object System.Drawing.Size(285,140)
$Form.MaximumSize = New-Object System.Drawing.Size(285,140)
$Form.StartPosition = "CenterScreen"
$Form.SizeGripStyle = "Hide"
$Form.Text = "Enter Computer Name"
$Form.ControlBox = $false
$Form.TopMost = $true
$TBComputerName = New-Object System.Windows.Forms.TextBox
$TBComputerName.Location = New-Object System.Drawing.Size(25,30)
$TBComputerName.Size = New-Object System.Drawing.Size(215,50)
$TBComputerName.TabIndex = "1"
$GBComputerName = New-Object System.Windows.Forms.GroupBox
$GBComputerName.Location = New-Object System.Drawing.Size(20,10)
$GBComputerName.Size = New-Object System.Drawing.Size(225,50)
$GBComputerName.Text = "Computer name:"
$ButtonOK = New-Object System.Windows.Forms.Button
$ButtonOK.Location = New-Object System.Drawing.Size(195,70)
$ButtonOK.Size = New-Object System.Drawing.Size(50,20)
$ButtonOK.Text = "OK"
$ButtonOK.TabIndex = "2"
$ButtonOK.Add_Click({Set-OSDComputerName})
Load-Form

Obtain serviceUI.exe from MDT 2012 and create a package

In order for the PowerShell script to be visible during a Task Sequence, we need to launch it with the help of a file called serviceUI.exe. That binary is available with MDT 2012 (you’d need to install MDT first). The file is located in these locations, depending on the architecture:
C:\Program Files\Microsoft Deployment Toolkit\Templates\Distribution\Tools\x64
C:\Program Files\Microsoft Deployment Toolkit\Templates\Distribution\Tools\x86
Depending on your boot image architecture, be sure to grab the correct version of serviceUI.exe (thanks to Donald for pointing out that an x64 boot image only works with the x64 version of serviceUI.exe). Copy the serviceUI.exe to the same folder you stored the Set-OSDComputerNamePrompt.ps1 file (in my case \\CM01\ContentLibrary$\OSD\Scripts\OSDComputerName).
52_1
Create a package in ConfigMgr 2012 SP1 and call it OSDComputerName (or something else). The package does not need to have any program, but specify the content source folder where you stored serviceUI.exe and Set-OSDComputerNamePrompt.ps1. Remember to distribute your package.

Update your boot image to support PowerShell

If you havn’t already updated your boot images with PowerShell support, follow these instructions.
1. Right-click on your boot image and select Properties.
2. Select the Optional Components tab.
3. Click on the yellow star and select the following components:

  • Microsoft .NET (WinPE-NetFx4)
  • Windows PowerShell (WinPE-PowerShell3)

Note! In ConfigMgr 2012 R2 the names of the optional components differ a bit. Add the following:

  • Microsoft .NET (WinPE-NetFx)
  • Windows PowerShell (WinPE-PowerShell)

4. Click OK and then OK again. Let the wizard update your DP’s.

Implement the script in a Task Sequence

1. Edit the Task Sequence that you wish to add this functionality to.
2. Add a Run Command Line step in between the Partition Disk and Apply Operating System steps.
3. Rename the Run Command Line step to Prompt for ComputerName.
4. Edit the Command Line so that it looks like this:

ServiceUI.exe -process:TSProgressUI.exe %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File Set-OSDComputerNamePrompt.ps1

5. Put a check mark in the Package box, click Browse and select your newly created package (OSDComputerName).
52_2

Results

When you execute the Task Sequence on a client during OSD, you’ll now get a prompt where you can enter the computer name for that particular system.
52_3
If you enter more than 15 characters or none at all, the script will give you a corresponding error.
52_5
Enter a computer name and click OK.
52_4
I hope this helps. Let me know if you find any bugs or have suggestions for additional functions.

(29116)

Nickolaj Andersen

Chief Technical Architect and Enterprise Mobility MVP since 2016. Nickolaj has been in the IT industry for the past 10 years specializing in Enterprise Mobility and Security, Windows devices and deployments including automation. Awarded as PowerShell Hero in 2015 by the community for his script and tools contributions. Creator of ConfigMgr Prerequisites Tool, ConfigMgr OSD FrontEnd, ConfigMgr WebService to name a few. Frequent speaker at conferences such as Microsoft Ignite, NIC Conference and IT/Dev Connections including nordic user groups.

47 comments

  • Hi (Hej) Nikolai
    I love this! The implementation of the script is exactly what I’ve been looking for,
    except in stead of asking for a computer name, I’d like the script to ask if the installation should continue or not.
    The script should only be called when the TS isn’t started from winPE and somehow make the TS fail/exit when “NO” is pressed..

  • Hello and manu thinks for your post.
    I have followed the steps to add teh script in the Task Sequence and i can see the popup to enter the new server name but i reboot the server and check the name, it’s still the old one.
    Do you have any idea about this problem please ?
    Thanks in advance for your assistance.

  • Hi. thanks I was able create drop down menu from powershell. it select computer type L-CITY=7 SECRIAL NUMBER AND CONNECTS TO SPECIFIC OUS.

  • Would anyone know how to use this exact script (where it prompts the user for text entry), but instead of settings the computer name, it sets the computer description? Ideally, I’d love to be able to set the computer name, then straight away prompt for the computer description. Any help would be greatly appreciated! πŸ™‚

  • Those receiving errors you likely need to specify the full path to powershell rather than using %systemroot%.
    “x:\windows\system32\windowspowershell\v1.0\powershell.exe”

  • Failed to run the action: Prompt for ComputerName.
    Unknown error (Error: FFFFFFFF; Source: Unknown)

  • Hi Nickolaj, thank you for this great post. I’m wondering is I can do kind of the same process but in different way, let said, instead to prom for “Computer name” could be possible prompt for “Domain user name” store it as a TS variable, then connect to AD to get user name, an so get the user department and from that generate a computer name following standards like e.g. Domain + Laptop or Workstation + department + consecutive number (MSWIT001). I’ll appreciate any help. Thank you,
    Tommy.

  • Failed to run the action: Prompt for ComputerName.
    Unknown error (Error: FFFFFFFF; Source: Unknown)

  • What’s the best way to restrict this step to only run on new computers that haven’t been named yet?

  • Hi Nickolaj,
    I am very new to creating task sequences. I have completed your step-by-step instructions and I am getting a 0x8007010B error which I understand could be bad or missing file path? I have double checked and everything looks good. When the sequence starts, the format window pops up for a moment before the server name pop up window even though I have the entry between Partition Disk 0 and Apply OS. Any thoughts would be greatly appreciated. Thank you.

  • This works without the need to have MDT and still having your Task Sequence deployed as Purpose Required. However, I didn’t use the form above and I wanted to point out that I just have a Read-Host in my script. No fancy nice window showing up mainly because I’m not a programmer and don’t understand how to create forms quickly. I also needed to install MDT on a machine just to get the file referenced above in the directions, but only for that purpose.
    Nonetheless, I couldn’t get the above to work for me until I noticed that when running PowerShell the -WindowStyle Hidden was in the command. Once I remove this (The default value of -WindowStyle being “Normal”) then finally the console of PowerShell would display with my Read-Host now able to be used!! πŸ˜€ Hopefully someone else may will find this information handy.

    • Hi Tim,
      Excellent! I’m glad that you were able to find a solution that suits your needs, and is easy for you to maintain, that’s the whole point really. And yeah, if you’re just running a command, you’d not use the -WindowStyle parameter unless your script is showing a UI or something.
      Regards,
      Nickolaj

  • Hi Nickolaj,
    I ended up using the code that Dave Green provided, and then adding some messagbox code to the beginning of that. I’m a total beginner when it comes to this sort of stuff, so I’m sure there’s probably a whole bunch of rules that I’ve broken by doing it the way that I have. It seems to be working the way that I wanted it to though, the only problem is getting it to appear on top! Here’s the code anyway: https://pastebin.com/2Gk6tsiQ
    Just trying to get this working has at least got me interested in learning PowerShell, so that’s a good thing at least πŸ˜€

    • Hi Matt,
      Awesome that you were able to create the code you were looking for. PowerShell is just great, isn’t it? πŸ™‚
      Thanks for sharing your code as well.
      Regards,
      Nickolaj

  • Thanks for this, has made my life so much easier! πŸ™‚
    I’ve been looking into creating an addition part to this, where you are given a yes/no box asking if you would like to change the computer name. If yes, then your prompt pops up where you can type the computer name. If no, then the sequence will continue, keeping the previous machine name. I was able to get this to work, but the box appears behind the task sequence window.
    I’m not particularly great with scripting, do you know how I would be able to create a box that stays at the top like your one does?

  • Regard candiandrum issue with the 0xFFFD0000 error. I had the same thing, and in an effort to troubleshoot, I had to enable the F8 command support on the winpe image, and then the error went away. Im not sure how this would fix it but thought I would share.

  • This guide is awesome! I have just started using PowerShell and I really want to add on to this script to make a check box or something to add the same computer to my domain at the same time that I rename it. Is that possible? If so how would I go about it?
    We don’t have SCCM setup or anything but this would speed up my re-imaging tremendously.
    Thanks!

    • Hi Blaine,
      I’m glad that you like it! PowerShell is truly king πŸ™‚
      I’m curious, why would want to create the domain computer account at the stage where you specify the computer name? The task sequence will at a later stage domain join the computer and when that happens, the computer account will be created automatically. How did you plan to leverage the script, in MDT or something?
      Regards,
      Nickolaj

      • Nickolaj,
        Thanks for the response! I figured out how to get what I wanted in MDT. Thanks!

  • Hello again!
    I have a problem with task sequence. It shows me an error “0xffffffff” on running command line step. Not for so long time I don’t had issues.

    • First time trying to set this up, and I’m getting the error too. Using SCCM 1606 and Windows 10.

    • I ran into this issue on a new deployment as well. The problem was that my boot image didn’t have the powershell and .net prereqs included.

  • Hi!
    How I can create a package? SCCM says that UNC path folder is empty.
    “Create a package in ConfigMgr 2012 SP1 and call it OSDComputerName (or something else). The package does not need to have any program, but specify the content source folder where you stored serviceUI.exe and Set-OSDComputerNamePrompt.ps1. Remember to distribute your package.”

    • Hi Mike,
      Creating a Package is one of the simplest tasks that every ConfigMgr administrator should know how to do. Here’s a high-level step-by-step:
      1. Right-click on the Packages node under Software Library\Application Management and select Create Package.
      2. Enter a name, description manufacturer, language and version. Check the ‘This package contains source files’ box and enter the UNC path to where you’ve stored the Set-OSDComputerNamePrompt.ps1 file. Click Next.
      3. On the Program Type page, select ‘Do not create a program’. Click Next and finalize the wizard.
      I hope this helps, if not let me know!
      Regards,
      Nickolaj

      • Thanks a lot!
        I make a first steps into SCCM, so from first look I was a little bit confused about creating a package.

  • Great tutorial. Thanks. Is it possible to set the OSDComputerName Environment Variable from within a 64 bit .NET C# application running within WinPE?

    • Hi Scott,
      As long as the required .NET Framework version that the application is based upon is available in the WinPE image, I don’t see any problems.
      Regards,
      Nickolaj

  • Awesome guide, just what I was looking for! I believe I did everything correctly but I may be missing something simple. Powershell opens and everything at the point it should, but I get error code 0xFFFD0000 when it seems to try running the script.

    • Hi,
      You’re welcome!
      Could you please provide a screenshot of how you’ve configured your TS and then a snippet from the smsts.log where it fails?
      Regards,
      Nickolaj

      • I think I got stuck on the same problem as canadiandrums, error code 0xFFFD0000.
        @Nickolaj, In the beginning of this guide you name the PS script “OSDComputerNamePrompt.ps1”. When you add the command line it’s named “Set-OSDComputerNamePrompt.ps1″…
        Other than that, thanks for a great guide πŸ™‚

      • It still has β€œSet-OSDComputerNamePrompt.ps1β€³. I got hung up on this.

    • Hi Dave,
      Thanks for contributing to this script! I’ve added a comment about it in the post, so that people can find it easier πŸ™‚
      Regards,
      Nickolaj

  • you don’t need to do this, just add the OSDComputerName as a collection variable on the collection you are deploying to.

  • RE: “If you’re using an x64 boot image to deploy Windows, any of the files should work (I’ve only tested the x64 version though)”
    FYI – I tested a 64 bit deployment with the 32 bit version of serviceUI.exe and it did not work. Once I swapped with the 64 bit version all was well. Great tutorial.

  • Hi There
    Great tutorial, but I was wondering if you had a customised WinPE image with Powershell enabled, as “%SYSTEMROOT%\System32\WindowsPowerShell”, if run from a TS would be pointing to “X:\”, which by default Powershell is not enabled in WinPE 4.0, and needs a custom image?

    • Hi Lyle,
      I’ve modified the WinPE image from within ConfigMgr and added the PowerShell and .NET components. When those are added, you’ll be able to launch powershell.exe with the command line provided in this guide. Actually, I see now that I havn’t mentioned that in this post. Will update it to reflect how my WinPE image is configured.
      Regards,
      Nickolaj

Sponsors

Subscribe

Do you want to be notified of new posts on our site?

Please enter your email address below:

Categories

MSEndpointMgr.com use cookies to ensure that we give you the best experience on our website.