Prompt hostname change command line - 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.

Related

How do I change the hostname of a Windows PC with batch or 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"

Running PS1 file from batch file, same folder on thumb drive

Admittedly I'm no scripter. I piece together what already works but trying to learn.
I have a script that does a lot of the manual labor for setting up a scan user for myself and our techs. Here is a small portion of it written as a batch file. At the end before the pause I want to call a PowerShell to show what the Network type is, not to change it. At least not at this time. I did remove alot of the extra from the file to save space. Both the batch file and the PS1 file will be in the same folder on a thumb drive.
The nettype.ps1 file just has:
get-netconnectionprofile
pause
The pause of course is so the tech can see the network type.
Hope someone has a simple solution. I did look here and other websites. I may not be using the right terminology in my search or understanding what I need done.
net user Scans Scanner1 /add
net localgroup administrators Scans /add
wmic UserAccount where Name='Scans' set PasswordExpires=False
md C:\Scans
#echo off
NET SHARE Scans=C:\Scans /Grant:Scans,Full
ICACLS "C:\Scans" /Grant Scans:(OI)(CI)(F) /t /c
ICACLS "C:\Scans" /Grant Everyone:(OI)(CI)(F) /t /c
netsh advfirewall firewall set rule group="Network Discovery" new enable=Yes
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes
PowerShell.exe -File "nettype.ps1"
pause
If that is all you have inside your powershell script, don't run it as a script, delete it and just run the command directly in your batch-file:
"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "Get-NetConnectionProfile"
Which could be probably be shortened to:
PowerShell Get-NetConnectionProfile
I found the answer, knew it would be simple.
Just had to use the following in the batch file:
powershell.exe -ExecutionPolicy Bypass -File ""%~dp0nettype.ps1""
You can change the powershell call to the following to find the ps1 file in the same directory:
powershell.exe -File "%~dp0nettype.ps1"
%~dp0 is a combination of %0 variable and ~d and ~p modifiers.
%0 is the full path to the current batch file.
~d when combined with %0 (e.g. %~d0) will get you drive letter portion (e.g. C:) from %0.
~p when combined with %0 (e.g. %~p0) will get you the path portion of %0 without the filename.
Combining them together, %~dp0, will get you the full path of the folder where current batch file is located.
You can find a complete list of these modifiers here: https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10)?redirectedfrom=MSDN
One thing to note, is that %~dp0 modifier only works in batch files, not when you try to run on commandline directly.

In Win10 Batch File's Arguments: Replacing UNC Path Names' prefixes with known mapped drive

I work in a shared drive on a server.
Sometimes people use its mapped drive, Q:\Folder\Subfolder\Etc\
Other times people use its UNC path: \\server.com\shared data\Folder\Subfolder\Etc\
I have a batch file that takes an argument of the paths of file(s) (usually multiple files) dropped onto it. It then passes those to a PowerShell script.
However, when someone drops files from a folder being access from its UNC path name, it barfs:
'\\server.com\shared data\Folder\Subfolder\Etc'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
How can I replace \\server.com\shared data\ with Q:\ for every filename in the argument? My PowerShell script does all the processing, so I want to fix it before it ever gets sent into PowerShell.
I get I can run 'cls' at the beginning of the beginning of the batch to clear away the warning.
I cannot modify the registry for this case.
#echo off
Title Pass arguments from this Batch script to PowerShell script of same name
rem
rem Batch Handler of Command Line Arguments for Microsoft Windows
rem Enables Passing Command Line Arguments to PowerShell Script
rem
rem The name of the script is drive path name of the Parameter %0
rem (= the batch file) but with the extension ".ps1"
set PSScript=%~dpn0.ps1
set args=%1
:More
shift
if '%1'=='' goto Done
set args=%args%, %1
goto More
:Done
powershell.exe -NoExit -Command "& '%PSScript%' '%args%'"
So, I'm hoping there is a way to keep the .ps1 PowerShell script from seeing any \\server.com\shared data\ and just let it see Q:\ instead.
This is what I ended up using: Enable Delayed Expansion was the ticket.
#echo off
setlocal enableDelayedExpansion
Title Sample Title (Initializing...)
rem
rem Batch Handler of Command Line Arguments for Microsoft Windows
rem Enables Passing Command Line Arguments to PowerShell Script
rem
rem
rem The name of the script is drive path name of the Parameter %0
rem (= the batch file) but with the extension ".ps1"
set "PSScript=%~dpn0.ps1"
set "args=%1"
:More
shift
if '%1'=='' goto Done
set "args=%args%, %1"
goto More
:Done
set "args=!args:\\server.com\shared data\=Q:\!"
if /i "!args!" == "\\server.com\shared data\=Q:\" ( set "args=" )
powershell.exe -NoExit -Command "& '%PSScript%' '%args%'"
Can’t try it right now, but I think you can try one of these:
Get all arguments’ names and parse \server.com\ to Q:\, not probably the best idea as each user could have different unit letter
If the files are on the same directory as the script (or always on same origin folder) try using pushd \\server.com\shared... to map the directory to a temp unit and then get it with cd to use it. When you are done, unmap the folder with popd (this will unmap last mapped unit)

command on specific device

I am trying to write a batch script, which will always be executed at a specific location (in this case on my USB-Stick), so typically I would use D:, but sometimes the stick has another drive letter. Therefore I am trying to find the device via its name (USB_Stick).
I haven't found a way to do this via a batch command.
A PowerShell command would look like this:
#(get-wmiobject -query \"select deviceid from win32_logicaldisk where volumename='USB-STICK'\")[0].deviceid"
but I don't know how to use the result of this PowerShell command.
I tried things like this:
for /f "usebackq" %%x in (`powershell.exe -Command "#(get-wmiobject -query \"select deviceid from win32_logicaldisk where volumename='USB-STICK'\")[0].deviceid"`) do (
set res=%%x
)
#echo %res%
but the result of this would only be ommands.GetWmiObjectCommand and not the D:.
If you're going for a batch-script anyway, use the wmic commandline utility:
#echo off
for /f "delims== tokens=2" %%d in (
'wmic logicaldisk where volumename^="USB-STICK" get deviceid /value'
) do set "deviceId=%%~d"
echo %deviceId%
Using the wmic with the /value parameter creates name=value lines as the output, which you can split in the for loop by defining = as the delimiter.
If I understand correctly, the batch file is running on the USB stick? If so, then the drive letter of the stick (without a slash) can be obtained via %~d0

How can I detect whether or not I am in powershell from a command line?

I am creating a standard windows BAT/CMD file and I want to make an IF statement to check whether or not this CMD file is run from PowerShell. How can I do that?
Edit: My underlying problem was that test.cmd "A=B" results in %1==A=B when run from CMD but %1==A and %2==B when run from PowerShell. The script itself is actually run as an old Windows Command line script in both cases, so checking for Get-ChildItem will always yield an error.
One way, it to see what your process name is, and then check its attributes:
title=test
tasklist /v /fo csv | findstr /i "test"
As long as you use a unique name in place of Test, there should be little room for error.
You will get back something like:
"cmd.exe","15144","Console","1","3,284
K","Running","GNCID6101\Athomsfere","0:00:00","test"
When I ran the above code from a bat file, my output was:
"powershell.exe","7396","Console","1","50,972
K","Running","GNCID6101\Athomsfere","0:00:00","
A potentially simpler approach that may work for you. If not, it may be suitable for others.
Create 2 separate script files: test.ps1 and test.cmd
Don't include extension when calling the script. I.e. call as <path>\test (or just test if folder is in the path environment variable.
This works because CMD prioritises which script to execute as: .bat > .cmd, whereas Powershell prioritises: .ps1 > .bat > .cmd.
The following is the output of a CMD session:
C:\Temp>copy con test.cmd
#echo cmd^Z
1 file(s) copied.
C:\Temp>copy con test.ps1
Write-Output "ps1"^Z
1 file(s) copied.
C:\Temp>.\test
cmd
C:\Temp>
And calling test from Powershell:
PS C:\Temp> .\test
ps1
PS C:\Temp>
Couldn't you try to execute a Get-ChildItem and then check %ERRORLEVEL% to see if it returns an exe not found?
http://ss64.com/nt/errorlevel.html