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
Related
I am doing some testing to run powershell script in cmd. I encountered the error when running the powershell script. Please advise how to rectify it.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Set "WinVerAct="
For /f "tokens=*" %%W in ('
cscript /Nologo "C:\Windows\System32\slmgr.vbs" /xpr
') Do Set "WinVerAct=!WinVerAct! %%W"
if Not defined WinVerAct (
Echo:No response from slmgr.vbs
Exit /B 1
)
Echo Windows Version Activation Status:
Echo:"%WinVerAct:~1%"
ping /n 5 localhost>nul 2>&1
Echo Check Status thru PowerShell
powershell -Command "Get-CimInstance SoftwareLicensingProduct -Filter "partialproductkey is
not null" | ? name -like windows*"
ping /n 5 localhost>nul 2>&1
wmic csproduct
ping /n 5 localhost>nul 2>&1
You're using two sets of double quotes in the powershell command calling Get-CimInstance and obviously as soon as you use the second one it closes that part rather than encompassing the whole expression.
Probably the easiest fix is to just use single quotes for the internal part:
powershell -Command "Get-CimInstance SoftwareLicensingProduct -Filter 'partialproductkey is not null' | ? name -like windows*"
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"
I've been trying to integrate a PowerShell script inside a Batch file but I seem to get some errors when trying to do so.
This is my code so far:
for /F "tokens=* delims=," %%A in (C:\MACs.log) do (
#ECHO OFF
PowerShell.exe "& arp -a | select-string "%%A" |% {$_.ToString().Trim().Split(" ")[0]} >> C:\tempfile.log
Ok so, when I run this command in PowerShell everything is working OK but in Batch I get some token errors.
The main post of this code is to create a loop from the MACs.log where I have a couple of MAC addresses that I want the IP addresses to and output them to tempfile.log.
If someone can lend a helping hand it would be greatly appreciated.
Why not directly solve it from the batch file?
(for /f %%a in ('
arp -a ^| findstr /l /i /g:"c:\MACs.log"
') do #echo %%a) > c:\tempfile.log
The inner pipe command will execute the arp and filter its output using the mac addresses found in MACs.log. This list is processed by the for /f command that will retrieve the first token in the line (default behaviour), that is, the ip address, and echo it. All the output is sent to a tempfile.
I have a path in variable (script parameter) %2.
I need to do the following:
Extract the leaf (last folder from the path) to a variable.
Run the following command: robocopy %2 \\somepath\%leaf
I was told this could be done in PowerShell (cause I've tried going with batch file alone and failed miserably) Here's a pseudocode representation of what I'd like to achieve:
set leaf = powershell -command (split-path %2 -leaf)
robocopy %2 \\somepath\%leaf
Any idea how to write this correctly?
Thank you.
Whenever you want to set a batch variable to the output of a command, use for /f. Here's an example:
#echo off
setlocal
set "psCommand=powershell -command "(split-path '%~2' -leaf)""
for /f "delims=" %%I in ('%psCommand%') do set "leaf=%%I"
echo %leaf%
But this is a terribly inefficient way to retrieve the last folder of a path. Instead of invoking PowerShell, what you should do is this:
#echo off
setlocal
for %%I in ("%~2") do set "leaf=%%~nxI"
echo %leaf%
The %%~dpnxI notation gets
d = drive
p = path
n = name
x = extension
It's traditionally intended for files, rather than directories; but it works just as well for directories anyway. See the last couple of pages of for /? in a console window for complete details.
FOR %%a IN ("%~2") DO FOR %%b IN ("%%~dpa.") DO ECHO %%~nxb
Batch one-liner. Take the parameter (second parameter here), remove any quotes and re-apply them. Select the drive and path, add '.' then select the name and extension of the result making leaf required.
Obviously, if you require this in a variable,
FOR %%a IN ("%~2") DO FOR %%b IN ("%%~dpa.") DO set "leaf=%%~nxb"
I have been searching and searching and have not come up with a good answer. I am trying to query the registry in both the locations for a software's UninstallString.
-HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall
-HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
The UninstallString needs to be set to a variable or piped straight in to run the msiexec.exe /x {GUID} /qn.
I would prefer this to be in powershell or command just my ease of use.
#ECHO OFF
SETLOCAL
set "uninstall1="
FOR /F "tokens=2*" %%A IN (
'REG QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion" /v Uninstall'
) DO (set Uninstall1=%%B)
set uninstall
This should retrieve the first string, if it exists.
Repeat the formula for the second string. The final SET uninstall is merely to report the environment variables starting UNINSTALL for verification.
Then
if defined Uninstall1 msiexec ... "%Uninstall1%"
should execute MSIEXEC with the parameters you require (where ... are your options - /x, /qn whatever)