Automating Windows 10 Installs

I hate repetitive tasks, but I love automating them. One of the most common tasks for me is installing Windows, so I sought to automate as much of that as I could. I just wanted to write about a custom command I made for my Windows 10 Answer File.

If you haven’t heard of Windows 10 Answer Files, you can read about them here. Long story short, they automate installs for things like Windows and Office. If you want to get make your own, you can easily get started by using a template from the unofficial Windows 10 Answer File Generator.

In my Answer File (I will now call it autounattend.xml) I automatically wipe the hard drive of the target computer, reformat it, add my company as the organization, name the initial user, set the language and time zone, enter the product key for Windows, add a basic password-less admin account, enable the Local Administrator account, set the Local Administrator password, and disables the temporary admin account.

After that things get fun.

I wanted to launch a prompt asking for the user to input a computer name, then name the computer and restart. Unfortunately documentation on this subject is incredibly scarce so I had to figure out a solution myself. Thankfully, I’m a stubborn person.

A couple limitations to autounattent.xml command capabilities:

  • Each command must be on a single line of code.
  • Each line of code can be no greater than 260 characters.
  • Local variables can not be set and used on the same line of code.

Ouch. Long story short, I found a solution: create a temporary container and set environment variables within that container, then close it. That way I can set a variable and use it on a single line of code.

<SynchronousCommand wcm:action="add">
          <Order>7</Order>
          <RequiresUserInput>true</RequiresUserInput>
          <CommandLine>cmd /V /C echo Computer name:&setlocal&set /p NewName=&powershell -Command "Add-Computer -domainname *********.com -ComputerName $env:computername -NewName $env:NewName -OUPath 'OU=Newly Configured Computers,OU=I. T.,DC=**********,DC=com'"&endlocal</CommandLine>
          <Description>Rename PC and Join Domain</Description>
</SynchronousCommand>

From there, Group Policy installs Chrome and UltraVNC with the desired configuration, customizes firewall settings and allows RDP. All that to say, deploying new computers is now a very simple process. An hour saved is an hour earned, right?

KB5000802 🤦🏼‍♂️

Anyone else have issues with Microsoft’s KB5000802 hotfix? It broke a lot of our printers. Fastest way to resolve this issue is to uninstall the hotfix. Since we needed to do it on hundreds of computers, I made a script for that. I hope this helps someone!

Instead of hosting this code on WordPress, I published it on my GitHub.

Remote Check Config

Ever want to quickly check the configuration of a computer on your network? With this script, you can get a ton of info in seconds! Hostname, OS name, original install date, boot time, domain, RAM info, Windows hotfix application, network card status, system uptime, processor info, and even hard drive space!

CheckConfig(remote).bat

cls
@pushd %~dp0
@echo off
ECHO ============================
ECHO REMOTE CONFIGURATION CHECK
ECHO ============================
ECHO ____________________________
color 0A
set /p "id=Enter IP of computer you want to check: "
ECHO Please wait... Checking system information.
:: Section 1: OS information.
ECHO ============================
ECHO OS INFO
ECHO ============================
systeminfo /s %id%
PSTools\PsInfo -d \\%id%
@popd
pause

:: Love, CJ Pollock

This script DOES require PSTools. The PSTools folder should be in the same directory as this Batch file in order to work properly. Here’s a download for everything you need in one place:

Download: Remote Check Config.zip
(right-click and Save Link As)

Who’s Logged In?

Ever want a quick and easy way to check which user is currently logged into a computer on your network? Look no further! Simply input the IP address or hostname of the device on your network, and off you go!

LoggedInUserQuery.bat

cls
@echo off
ECHO ============================
ECHO WHO'S LOGGED IN?
ECHO ============================
ECHO ____________________________
color 0A
set /p "id=Enter IP or name of computer you want to check: "
query user /server:%id%
PAUSE
:: Love, CJ Pollock

Remote Pair Shared Drive

We’ve had an issue where users in our company would request access to a shared drive, but they weren’t at their computer when our IT department was available to log in and map it. To solve this, I made a script that will create a Batch file in a specified users’ Startup Folder. After the Batch file runs, it deletes itself.

remotePairNetworkDrive.bat

cls
@pushd %~dp0
@echo off
ECHO ============================
ECHO REM MAPPING TOOL
ECHO ============================
ECHO ____________________________
color 0A
:top
cls
ECHO ============================
ECHO REM MAPPING TOOL
ECHO ============================
ECHO ____________________________
color 0A
echo What computer (name or IP) do you want to run this on?
set /p address=
ping -n 1 "%address%" | findstr /r /c:"[0-9] *ms"
if %errorlevel% == 0 (
    GOTO:one ) else (
    echo [41mUnable to find %address%. Device may be offline. Try again?[0m
    pause
    GOTO:top
    )
:one
cls
ECHO ============================
ECHO REM MAPPING TOOL
ECHO ============================
ECHO ____________________________
color 0A
echo What computer (name or IP) do you want to run this on? %address%
echo What user do you want this installed for?
set /p user=
echo What is the address for the drive you'd like to pair?
set /p drive=
echo What letter would you like to map the drive to? Leave blank if any.
set /p letter=
if not defined letter (
    GOTO any
) ELSE (
    goto specified
)
:any
set command=net use * "%drive%" /YES
set /a r = %random%
echo @echo off > "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%r%.bat"
echo %command% >> "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%r%.bat"
TYPE "deleteScript.txt" >> "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%r%.bat"
GOTO two
:specified
set command=net use "%letter%:" /YES
GOTO two
echo @echo off > "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%address%.bat"
echo %command% >> "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%address%.bat"
TYPE "deleteScript.txt" >> "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%letter%.bat"
:two
popd
cls
:three
ECHO ============================
ECHO REM MAPPING TOOL
ECHO ============================
ECHO ____________________________
echo %drive% has been mapped on %address% for %user%.
echo Please have %user% restart %address% to see changes.
pause

:: Love, CJ Pollock

IMPORTANT: You need to use this next bit to make sure the target computer’s startup script deletes itself after execution. Simply name it deleteScript.txt and put it in the same directory as the previous Batch file:

deleteScript.txt

(goto) 2>nul & del "%~f0"

Download: remotePairNetworkDrive.zip
(right-click and Save Link As)