ok so I have the following Windows Batch Script that pulls information for all connected storage devices (internal HDD / external USB drives) which is working just fine. However, I would like to add the name of each detected drive after where it says: Volume x:\
like so: Volume x:\ NAME OF HDD HERE
.
call :setESC
call :hdd-info
endlocal
echo. Press any key to exit &>nul timeout /t -1 &exit /B
REM - Setup ANSI Escape Character ------------------------------------------------
:setESC
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set ESC=%%b
exit /B 0
)
REM ------------------------------------------------------------------------------
:: - Get Storage / HDD Info ------------------------------------------------------
:hdd-info
:: Enable use of Unicode symbols
chcp 65001 >nul
set "GB=1073741824"
for /f "skip=1 delims=" %%i in ('%SystemRoot%\System32\wbem\wmic.exe logicaldisk get deviceid^,freespace^,size^') do (for /f "tokens=1-3" %%j in ("%%i") do call :output %%j %%k %%l)
:: Disable Unicode symbols
chcp 1252 >nul
goto :eof
:output
if "%3"=="" (
:: echo. Volume %optical_drive_letter%\ - %optical_drive_type% ^(%optical_drive_name%^)
goto :eof
)
for /f "tokens=1-4" %%i in (
'mshta vbscript:Execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(FormatNumber(%3/%GB%, 2) & "" "" & FormatNumber((%3-%2)/%GB%, 2) & "" "" & FormatNumber(%2/%GB%, 2) & "" "" & Round((%3-%2)*50/%3)):Close"^)'
) do (
set "size=%%i"
set "used=%%j"
set "free=%%k"
set /a "nUsed=%%l, nFree=50-%%l"
)
echo( Volume %1\
echo( Total Size : %size:~-10%GB
echo( Used Space : %used:~-10%GB
echo( Free Space : %free:~-10%GB
for /l %%i in (1 1 %nUsed%) do <nul set /p "=%ESC%[91m▓%ESC%[30m"
for /l %%i in (1 1 %nFree%) do <nul set /p "=%ESC%[92m▓%ESC%[30m"
echo( &echo(
goto :eof
:: ------------------------------------------------------------------------------
The output is as follows: https://imgshare.io/image/list-hdds.rN55xP
Now, I also have the following that shows Optical drives as an example of the layout/information I need. IE: The name of the device adding after the Volume label.
PLEASE NOTE This code is simply here as an example only.
:: - Get Optical Drive(s) Information -----------------------------------------------
for /f "skip=2 tokens=2,3 delims=," %%i in (
'%SystemRoot%\System32\wbem\wmic.exe logicaldisk where "drivetype=5" get Caption^ /format:csv'
) Do (
set "optical_drive_letter=%%i"
)
for /f "tokens=2,3 delims=," %%a in (
'%SystemRoot%\System32\wbem\wmic.exe cdrom where "mediatype!=11" get name^,mediatype /format:csv'
) Do (
set "optical_drive_type=%%a"
set "optical_drive_name=%%b"
)
::echo( Volume %optical_drive_letter%\
::echo( %optical_drive_description%/%optical_drive_type% (%optical_drive_name%)
:: ------------------------------------------------------------------------------
echo( Volume %optical_drive_letter%\
echo( %optical_drive_type% (%optical_drive_name%)
echo(
So, to clarify the above displays the Volume label as does the initial script. However, it also shows the device name too.
https://imgshare.io/image/list-optical.rN5bbu
I know I can use wmic.exe logicaldisk get Caption
to get this in CMD so can use something like this in a batch-file:
for /f "delims=^= tokens=2" %%a in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_DiskDrive GET Caption /format:list ^| findstr ^=') do set logicaldisk_name=%%a
My issue is that I do not know how to alter the original script to also get and show each devices name for each physical drive that is detected.
Can anyone please help with this as I seem to be pulling my hair out trying to make this work for all connected drives?
Full script can be found here: https://pastebin.com/q5QS4EmF
Many thanks,
Bri
CodePudding user response:
Without launching WMIC.exe
, and mshta.exe
, (to call out to the VBScript
engine); and to incorporate the much better powershell.exe
, as offered in the other answer, here's an example batch-file you could play around with, or learn from.
@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile ^
"Get-CimInstance -ClassName Cim_Diskdrive -PipelineVariable Disk | "^
"ForEach-Object { $_ | Get-CimAssociatedInstance -ResultClassName Cim_DiskPartition -PipelineVariable Partition } | "^
"ForEach-Object { $_ | Get-CimAssociatedInstance -ResultClassName Cim_LogicalDisk } | "^
"Format-Table @{ Label = 'Letter'; Expression = { $_.DeviceId } }, "^
" @{ Label = 'Model'; Expression = { $Disk.Model.Trim() } }, "^
" @{ Label = 'Capacity'; Expression = { '{0:F0} GB' -F ($Disk.Size / 1000000000) } }, "^
" @{ Label = 'Partition'; Expression = { $Partition.Index } }, "^
" @{ Label = 'Label'; Expression = { $_.VolumeName } }, "^
" @{ Label = 'Size'; Expression = { '{0:F1} GiB' -F ($_.Size / 1GB) } }, "^
" @{ Label = 'Used'; Expression = { '{0:F1} GiB' -F (($_.Size - $_.FreeSpace) / 1GB) } }, "^
" @{ Label = 'FreeSpace'; Expression = { '{0:F1} GiB' -F ($_.FreeSpace / 1GB) } }, "^
" @{ Label = 'PercentFree'; Expression = { '{0:P1}' -F ($_.FreeSpace / $_.Size) } } -AutoSize"
@Pause
I am aware that you are hoping for answers which you could just incorporate into your posted code submission, but in order to get the result you require, your entire script would need to be re-written and the resulting code would be significantly slower, more complex, and very difficult to understand and adapt.
CodePudding user response:
If I understand your question correctly, you would like to identify the drive letter, volume name, and disk usage properties for all attached logical disks.
- Open PowerShell ISE.
- Paste the following code in the script pane.
- Press play (green arrow).
$DriveInformation = Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object DeviceID, VolumeName, Description, @{'Name' = 'Total Size (GB)'; Expression = {[int]($_.Size/1GB)}}, @{'Name' = 'Used (GB)'; Expression = {[int](($($_.Size)-$($_.FreeSpace))/1GB)}}, @{'Name' = 'Available (GB)'; Expression = {[int]($_.FreeSpace/1GB)}}
$DriveInformation
Bonus
If you want to export it to a file you can pipe the results of $DriveInformation to a specific format. Example is below;
$DriveInformation | Export-Csv "C:\temp\driveinformation.csv" -NoTypeInformation -Force
Start-Process "C:\temp\driveinformation.csv"
Edit1:
I should note that you can save the script as a .ps1 which can be run just like a batch file.