Join to Domain through USB?

HI everyone,

I need to join different computers to a domain. I always did it through wmic commands in a bat file, no problem till now. Now the wmic command is deprecated (Win11) and i’m trying to find a way to join.

before i used this:

wmic computersystem where name="%computername%" call joindomainorworkgroup fjoinoptions=3 name=domain username=domain\user Password="password"

Now i’m trying to accomplish it through powershell that looks like this:

$domain = "domain"

$password = "password" | ConvertTo-SecureString -asPlainText -Force

$username = "$domain\user"

$credential = New-Object System.Management.Automation.PSCredential($username,$password)

Add-Computer -DomainName $domain -Credential $credential

The script works when launched from an administrative powershell session. But i’d like to run it from my USB. I tried/encountered the following errors:

1-Execution Policy

I don’t want to disable execution policy for each machine, but just temporarily. I worked around this doing a .bat file that calls the .ps1 file bypassing execution policy:

powershell -ExecutionPolicy Bypass -File script.ps1

The script run, but it says “access denied” because it needs an administrative powershell session.

2-Executing Powershell as admin user??

The second and most annoying one is that the script won’t run in an admin console, even if the bat file has run as admin. So i tried doing this bat file:

powershell -noexit -ExecutionPolicy Bypass -Command "Start-Process -Verb RunAs powershell -ArgumentList '-ExecutionPolicy Bypass -File .\joinwsm.ps1'"

But the script just won’t do anything. If I remove the “ArgumentList” part, it correctly opens a powershell terminal window as admin.

I’ve been trying for too much time right now, maybe the thing is simpler than i think :slight_smile:

Any help/suggestion would be appreciated, thank you!

All you need is
Add-Computer -Credential $(Get-Credential)

The credential will be those with privs to join a computer to the domain

Hi, that’s not the case. It’s not a problem with my user (the join script works)
The problem is with runninr that script in an administrative powershell session from a USB device

Hi @dzdz,

Is the code blocked on the USB drive? If a script is uploaded to somewhere it wasn’t originally created (in a run time environment such as being downloaded from GitHub or another device), the code can be blocked and prevented from running. Check this first and let us know.

Hi everyone, I found the solution!
Thank you @Austin_H , but that was not the problem, it was much simplier than I thought! The script in fact works BUT it opens an administrative shell in C:, and couldn’t find the script passed in the arguments cause it is on my usb stick (D:\ in this case). SoI simply specified the path for my script and now it looks like this:

powershell -noexit -ExecutionPolicy Bypass -Command "Start-Process -Verb RunAs powershell -ArgumentList '-ExecutionPolicy Bypass -File D:\joinwsm.ps1'"

Powershell was not the problem in the end :slight_smile:

Thank you all for your help!