How do I change the hostname of a Windows PC with batch or powershell - powershell

I want to change the hostname of a computer from Batch (a.k.a. Command Prompt) or Powershell.
Initially I started research into using the wmic command. But running wmic /? on Windows 10 21H1 indicates it is now deprecated.
Then I looked at Get-WmiObject. But when I run man Get-WmiObject in PowerShell, the description indicates it has been "superseded" by Get-CimInstance.
Using the old Get-WmiObject command you could change your own computer's hostname with (Get-WmiObject Win32_ComputerSystem).Rename("New-Hostname").
What is a non-deprecated way to change your own Windows computer's hostname using Batch or PowerShell?

Thanks to #Theo for the tip.
The PowerShell command Rename-Computer
Rename-Computer "new-hostname"
Admin privileges and a computer restart are required.
The command warns you if the length of the hostname is longer than 15 characters.

The batch-file command...
NETDOM RENAMECOMPUTER "%ComputerName%" /Newname:"NewNameGoesHere" /FORCE

I made a simple code if you want to use it
#echo off
set /p newname=The name of the new device:
wmic computersystem where name="%computername%" call rename name="%newname%"
Anyway, this is what you are looking for
wmic computersystem where name="%computername%" call rename name="newname"

Related

Is there a way to execute powershell commands remotely on a domain user from the DC?

Let's say for example, I have a domain controller and a client that is joined to the domain.
If I wanted to remotely lock out the client I would supposedly run
Invoke-Command -ComputerName [workstation name] -ScriptBlock {rundll32.exe user32.dll, LockWorkStation}
However this does not work. I'm assuming this is because the Invoke-Command cmdlet runs the code in the scriptblock but returns anything back to the local terminal. What I'm trying to accomplish is to have the code or powershell script run locally on the remote computer.
My question is first of all if this is the correct approach and second why the command I'm running does not work.
Download PsExec from https://learn.microsoft.com/en-us/sysinternals/downloads/psexec and run following command.& "C:\PSTools\PsExec.exe" -s -i \\COMPNAME rundll32.exe user32.dll,LockWorkStation
As per my comment when using PSExec... So, stuff like this ---
PsExec.exe \\ -d -u \Administrator -i cmd.exe /c "C:\windows\system32\rundll32.exe user32.dll, LockWorkStation"
Or using PowerShell with quer.exe ...
(it's a tool in every modern Windows version)
quser | Microsoft Docs
...in a PowerShell remoting script, like described here:
How To Log Off Windows Users Remotely With PowerShell
Again the work is being done by quser.exe, not PowerShell specifically. PowerShell is just being used to run quser.exe remotely. You could do the same, by copying PSExec to the remote host and do a similar operation.

Powershell printer script

I will translate my question in english so that everyone can read it!
Am not really good in powershell i work on it for 3 days.
I need to create a powershell script to show what is the default printer selected in the computer ( example :PrinterA) before the execute a line to select another printer as default printer ( PrinterB) .
After that i need to reset the old default printer ( PrinterA).
I execute line:
Get-WmiObject -query " SELECT * FROM Win32_Printer WHERE Default=$true"
to show default printer but i dont know how to memorize it.
To select the (printeB ), I do:
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "PrinteB"
Can you help me please ?
I'd use
$OldDefaultPrinter = (Get-WmiObject win32_printer | Where-Object Default -eq $True).Name
To store the current default printer in a variable.
To restore with your method
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "$OldDefaultPrinter"
you did not mention the specific version of Powershell that you are using. [grin] presuming you are running ps5.1 on win10, you can use the print management cmdlets to do what you need. take a look at this ...
PrintManagement
— https://learn.microsoft.com/en-us/powershell/module/printmanagement/?view=win10-ps
the Get-Printer cmdlet will get info about the available printers. the Set-Printer cmdlet will let you set the default printer.

I am trying to run a batch command from PowerShell

I have run into a character limitation for net.exe
net localgroup administrators "domainname\G-%COMPUTERNAME%-LocalAdmins" /add
I have been told you can run this through PowerShell, but I have been unsuccessful. Here is what I have come up with:
powershell -command "& {([adsi]'LDAP://./localgroup,administrators').Add('LDAP://domainname/G-%COMPUTERNAME%-LocalAdmins,Administrators');}"
Any assistance would be greatly appreciated.
You can run external commands (like net.exe) from PowerShell practically the same way you can run them from CMD. However, if you want to use (environment) variables you have to use PowerShell variable syntax ($var or $env:var respectively). PowerShell doesn't recognize CMD/batch variable syntax (%var%).
This should work:
net localgroup administrators "domainname\G-${env:COMPUTERNAME}-LocalAdmins" /add
After a day or so of research I found a microsoft forum by Jaap Brasser. He has written a script that I could invoke with my parameters and get to work...
powershell.exe -ExecutionPolicy Bypass -file "\server\pub\DomainAdmin\SetADAccounts.ps1" -Computer %COMPUTERNAME% -Trustee domainname\G-%COMPUTERNAME%-LocalAdmins
Thank you for your input..
Per Microsoft:
The NET.EXE command does not support names longer than 20 characters for reasons of backward compatibility with LAN Manager 2.0.
Yes, Windows commands will pass variables to PowerShell. If the member is on the same domain, it does not need to be included. Use the Add-LocalGroupMember cmdlet.
PowerShell Add-LocalGroupMember -Group Administrators -Member 'G-%COMPUTERNAME%-LocalAdmins'

Powershell remotely register COM dll by using regsvr32

I found on Internet, this ps script may work. But the result I get is: no error pops up, but also DLL not found in registry after running the script.
Invoke-Command -ComputerName $servername -ScriptBlock {regsvr32.exe "\\uncpath\some.dll" }
I tried in both "run as administrator" and normal PS console window, and windows remote management service is on on remote server.
Any idea?
You need to use the silent option of regsrv32 (/s):
Syntax
REGSVR32 [/U] [/S] [/N] /I:[CommandLine] DLL_Name
Key /u Unregister Server.
/s Silent, do not display dialogue boxes.
/i Call DllInstall to register the DLL.
(when used with /u, it calls dll uninstall.)
/n Do not call DllRegisterServer, you must use this option
with /i.
CommandLine An optional command line for DllInstall
/c Console output (old versions only).

Prompt hostname change command line

The code:
wmic computersystem where name="%COMPUTERNAME%" call rename name="NEW-NAME"
How do I change it to allow user input for the new name?
set /p new_name=enter a new computer name :
wmic computersystem where name="%COMPUTERNAME%" call rename name="%new_name%"
Could you try this?
My exact batch file contents that worked for me are below:
set /p new_name=enter a new computer name :
wmic computersystem where "name='%COMPUTERNAME%'" call rename name="%new_name%"
...
for a constant (JLT-) and a variable name. I used the following
set /p new_name=enter a new computer name :
wmic computersystem where "name='%COMPUTERNAME%'" call rename name=JLT-"%new_name%"
I then placed the batch file in run once, to change the computer name post first boot. As of windows 10 you can no longer do at as part of the first boot in windows 10 or in the unattend.xml in sysprep.