PowerShell Commands in Batch
brucine Posted messages 24668 Registration date Status Member Last intervention -
Hello,
I don't understand anything about PowerShell, I only use a few commands in Batch when it's easier or when the Batch option doesn't exist.
The return works perfectly, including when the PowerShell command includes a pipe, like here (and of course, the variable %letter% corresponding to the successive partition letters has been defined previously).
powershell.exe -command "& { get-volume -DriveLetter %letter% | Format-Table -HideTableHeaders }">>%~dp0_tailledisque.txt However, I run into issues when trying to integrate this, which works perfectly in the PowerShell terminal:
[System.IO.DriveInfo]::GetDrives() | Format_Table
I have a doubt about the command interpreter hitting the brackets, if I want to test with an ECHO command by redirecting this "text" to a txt or ps1 file, I run into problems unless I escape them:
echo ^[System.IO.DriveInfo^]::GetDrives() ^| Format_Table>>%~dp0test.txt
But I'm back to square one if I put this in the braces of powershell.exe, it crashes on those escape characters.
It's not fundamental, I get most of the desired fields by another Cmdlet without "special characters", but to resolve this type of issue in the future, what is the correct syntax?
Thank you.
6 answers
Hello,
In Powershell, to redirect output, you need to specify which stream is being redirected:
get-volume -DriveLetter C|Format-Table -HideTableHeaders 1>out.txt
The streams range from 1 to 6 or *: https://learn.microsoft.com/fr-fr/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-7.4
In the last 2 commands you have Format_Table while it should be Format-Table. And perhaps the result is not considered a table since it's an object list: value.
-
Disable office
at 02:45 -
Reinstallation of w11 impossible after pc crash...
at 01:54 -
I would like to know how to report a scam
at 00:54 -
Site unreachable
on 23 Jun -
Nonexistent sent items folder
on 23 Jun -
Transfer mp3 from pc to mobile
on 23 Jun -
Increase the capacity of free's zimbra webmail to 10gb
on 23 Jun -
Unrefreshed usb drive on android interface
on 23 Jun -
Tencent impossible to remove
on 23 Jun -
Is there enough storage space on my vps?
on 23 Jun
Hello,
My apologies, I had written the dash correctly in the PowerShell console otherwise I would never have received the feedback, I don't know what I did when writing the command in a Batch without ever seeing the dash error...
Thank you.
While I have you and if I may take the liberty to ask, why is the result correct in PowerShell but not in Batch?
powershell.exe -command "& { Get-CimInstance win32_logicaldisk | where caption -eq "C:" | foreach-object {write " $($_.caption) total $('{0:N2}' -f ($_.Size/1gb)) Go libre $('{0:N2}' -f ($_.FreeSpace/1gb)) Go"} }I don't know, often managing CRLF is already not simple, so stacking two different environments on top of that...
To get the same result in batch, you need to use write-host instead of write.
Nickel, I'm a real klutz in a lot of things, PowerShell being one of them.
Just a small detail if possible, once I’ve looped this, it doesn’t tabulate:
I do have a Plan B via:
powershell.exe -command "& { Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'Total (Go)'; Expression= {[math]::round($_.Size/1GB, 2)}},@{'Name' = 'Libre (Go)'; Expression= {[math]::round($_.FreeSpace/1GB, 2)}} }"there it’s well tabulated, but is there a way to get rid of the header lines?
I’ve tried a ton of other formulas but either they don’t tabulate, or they write GB in the Anglo-Saxon way, or they deprive me of decimals...
I know, we can always do arithmetic on bytes and I have a functional solution but I’d rather avoid it, it’s as long as a day without bread when you have to test say from 10 To to hundreds of Mo to be zealous since at each step you have to limit the output to 9 digits, check based on the original string length if the same length is not straddling two units depending on whether the remaining 9 digits are greater than or not than 1024^3 and put the comma back, number of characters before and after in the right place.
Thank you.
You can always add the format-table
Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'Total (GB)'; Expression= {[math]::round($_.Size/1GB, 2)}},@{'Name' = 'Free (GB)'; Expression= {[math]::round($_.FreeSpace/1GB, 2)}} | Format-Table -HideTableHeadersAlmost perfect: in the basic formula, it was me who added the "Total (Go)" jargon which then replaced the standard one in the header line.
Now that we've removed them, I no longer have headers, but I also no longer have, like in the first image in <6>, what each value corresponds to (Total or Free) and the unit of measurement on each line.
What should I do to integrate them into the "Expressions" (tabulated, of course...)?
Thank you.
you can try the tab character `t
Get-CimInstance win32_logicaldisk | where caption -eq "C:" | foreach-object {write "`t $($_.caption) total`t $('{0:N2}' -f ($_.Size/1gb)) Go free`t $('{0:N2}' -f ($_.FreeSpace/1gb)) Go"}Thank you for the existence of the tab character, I might finally get to learn PowerShell.
Regarding looping through all drives to get the information for a drive on the same line, as barnabe0057 indicated, you need to replace write with write-host which gives us, once the tab before the drive letter is removed and the one for "Free" moved after the word "Go" from "Total":
Much better otherwise the size difference of the drives continues to generate a shift between the number and "Go" that I can't seem to get rid of.
As indicated in the second formula and image in <6>, I get a correct tabulation without the units but since the formula is a single entity for all drives there is a blank line after and a blank line before followed by two lines of titles.
We arrive at "the right solution" by redirecting the output to a file where we filter out the excess lines and tabulate the output but it's a pity, it's less "elegant" than a single raw PowerShell formula would be:
@echo off CHCP 65001>NUL powershell.exe -command "& { Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'Total'; Expression= {[math]::round($_.Size/1GB, 2)}},@{'Name' = 'Free'; Expression= {[math]::round($_.FreeSpace/1GB, 2)}} }">>%~dp0_lect.txt FOR /F "tokens=1-3 skip=3 delims= " %%A IN ('type "%~dp0_lect.txt"') DO ( SET lecteur=%%A SET total=%%B SET libre=%%C CALL :DISPLAY ) GOTO END :DISPLAY echo %lecteur% %total% Go %libre% Go exit /b :END pause del ~dp0_lect.txtTo align the different sizes, it's not rocket science:
@echo off setlocal enableextensions enabledelayedexpansion CHCP 65001>NUL set "pow=Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'Total'; Expression= {[math]::round($_.Size/1GB, 2)}},@{'Name' = 'Free'; Expression= {[math]::round($_.FreeSpace/1GB, 2)}}" call :routine pow pause exit /b :routine FOR /F "tokens=1-3 skip=3 delims= " %%A IN ('powershell.exe -command "& { !%~1! }"') DO ( SET "drive=%%A" SET "total= %%B" SET "free= %%C" echo. !drive! !total:~-10! Go !free:~-10! Go ) exit /bWhat is more complicated is converting GB to TB if necessary.
This is exactly what I ended up doing in the script in <11> after struggling for a while with PowerShell, which I am frustrated with, going through a CALL routine because I am also frustrated with delayed expansion variables.
In what @barnabe0057 proposes, what seems particularly interesting is the left truncation of a variable that allows aligning numbers of different lengths; you've been working on that alignment for a while, haven't you?
Yes, absolutely, I looked too quickly and became obsessed with the alignment of the columns of numbers and the unit Go without shifting the entire line according to the size.
In <11>, my numbers are aligned to the left, which is not a big deal, where his proposal is more orthodox by aligning them to the right (with just one anomaly for the SD card in N: which shows 59.3 instead of 59.30 and should actually read 59.60 (64 Go/1,024^3)-59.60, regarding a blank card where I copied a file of 1 Ko otherwise some commands fail where the explorer returns 59.4-59.22, the mysteries of Windows by the way are of no importance...).
I found not a very elegant solution but that gives the desired format: the ps1 are created in echo from the batch and then redirected to a first text file that I eventually redirect to a second one, removing the titles and the empty lines (the most annoying one is the one automatically generated at the end of the file corresponding to the "carriage return").
The presentation looks good.
I am interested in your script, would you share it?
I have an old script of this kind that I would like to update with yours.
Here, as I told you, it's effective but not very orthodox.
https://www.cjoint.com/c/NDbjPBonshF
Thank you for sharing.
I added my own touch to it a bit: https://pastebin.com/3JUD7WW0
.
I still need to merge it with my own tool so that it becomes my future Swiss army knife.
You will show me that, but I can't see the intermediary sauce, I'm getting thrown 403 unauthorized access.
Here is the code:
@echo off Mode con cols=105 lines=40 ::WARNING ::PARTITIONS OR DISKS MUST HAVE A NAME, THERE MUST NOT BE ANY SPACE setlocal enableextensions enabledelayedexpansion CHCP 65001 >nul IF NOT "%os%"=="Windows_NT" ( echo Error...Invalid OS... pause exit ) call :module rem powershell.exe -ExecutionPolicy Bypass -File "%~dp0fonctions.psm1" :: ------------------------------------------------------------------------------------------- :: displaying system information :: ------------------------------------------------------------------------------------------- echo. & echo ^[103;91mSearching Computer "%COMPUTERNAME%" Please wait.... [0m echo. ::Computer_Name FOR /F "tokens=2 delims==" %%A in ('wmic os get CSName /value') DO SET "nomPC=%%A" ::Model FOR /F "tokens=2 delims==" %%A in ('wmic computersystem get Manufacturer /value') DO SET "manufacturer=%%A" FOR /F "tokens=2 delims==" %%A in ('wmic computersystem get Model /value') DO SET "model=%%A" FOR /F "tokens=2 delims==" %%A in ('wmic bios get SerialNumber /value') DO SET "serialnumber=%%A" ::OS FOR /F "tokens=2 delims==|" %%A in ('wmic os get Name /value') DO SET "osname=%%A" FOR /F %%A in ('wmic os get OSArchitecture ^| find /i "bit"') DO SET "osarchi=%%A" FOR /F "tokens=4" %%A in ('systeminfo ^| find /i "Version" ^| find /v "BIOS"') DO SET "osversion=%%A" FOR /F "tokens=2 delims==" %%A in ('wmic os get ServicePackMajorVersion /value') DO SET "sp=%%A" FOR /F "tokens=3" %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "DisplayVersion"') DO SET "Version=%%A" ::Processor Name FOR /F "tokens=2 delims==" %%A in ('wmic cpu get name /value') DO SET "nomproc=%%A" FOR /F "tokens=2 delims==" %%I in ('wmic cpu get numberofcores /value') DO SET "cores=%%I" echo Computer name: %nomPC% echo Model: %manufacturer% %model% echo Processor Type: %processor_architecture% %nomproc% %cores% core(s) echo Serial Number: %serialnumber% echo OS: %osname% %osarchi% bits Service Pack %sp% echo Version: %Version% - NT= %osversion% echo Boot: ^< %FIRMWARE_TYPE% Mode ^> :: ------------------------------------------------------------------------------------------- :: using Powershell commands :: ------------------------------------------------------------------------------------------- set "pow1=Get-WMIObject win32_ComputerSystem | foreach-object {write-host " Total Memory: $('{0:N2}' -f ($_.TotalPhysicalMemory/1gb)) GB"}" set "pow2=Get-WMIObject Win32_OperatingSystem | Measure-Object -Property FreePhysicalMemory -Sum | %% {[Math]::Round($_.sum/1024/1024, 2)}" set "pow3=Get-PhysicalDisk | Select DeviceID, Size, BusType, MediaType, Model | Format-Table -HideTableHeaders" set "pow4=Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'Total'; Expression= {[math]::round($_.Size/1GB, 2)}},@{'Name' = 'Free'; Expression= {[math]::round($_.FreeSpace/1GB, 2)}}" call :disques pow3 call :partitions pow4 call :memoire pow1 pow2 del /f /q "%~dp0fonctions.psm1" echo. & pause exit :: ------------------------------------------------------------------------------------------- :: routine for retrieving disks :: ------------------------------------------------------------------------------------------- :disques echo. ECHO ^[104;93mDisk No, Bus, Type, Size, Name : [0m FOR /F "tokens=1-4,* delims= " %%A IN ('powershell.exe -command "& { !%~1! }"') DO ( FOR /F "tokens=1,2 delims= " %%H in ('powershell.exe -command "& { Import-Module %~dp0fonctions.psm1; ConvertUnits %%B }"') DO ( call :alignment "%%H" size set "size=!size:~-6! %%I" ) echo %%A %%C %%D !size! "%%E" ) echo. ECHO ^[104;93mDisks and partitions : [0m FOR /F "delims=" %%A IN ('powershell.exe -command "& { Import-Module %~dp0fonctions.psm1; PartitionInfos }"') do echo %%A | findstr ": ," goto :eof :: ------------------------------------------------------------------------------------------- :: routine for retrieving partitions :: ------------------------------------------------------------------------------------------- :partitions echo. ECHO ^[104;93mPartition occupancy : [0m FOR /F "tokens=1-3 skip=3 delims= " %%G IN ('powershell.exe -command "& { !%~1! }"') DO ( SET "drive=%%G" call :alignment "%%H" total call :alignment "%%I" free echo. !drive! ^>^>^> Total / Free : !total:~-8! / !free:~-8! GB ) goto :eof :: ------------------------------------------------------------------------------------------- :: routine for retrieving memory size :: ------------------------------------------------------------------------------------------- :memoire FOR /F "tokens=3 delims=: " %%A IN ('powershell.exe -command "& { !%~1! }"') DO CALL :alignment "%%~A" RamTotal FOR /F "tokens=1" %%A IN ('powershell.exe -command "& { !%~2! }"') DO CALL :alignment "%%~A" RamFree echo. ECHO ^[104;93mRAM Memory : [0m echo. Total Memory : %RamTotal:~-7% GB echo. Free Memory : %RamFree:~-7% GB goto :eof :: ------------------------------------------------------------------------------------------- :: routine for aligning sizes (left truncation) :: ------------------------------------------------------------------------------------------- :alignment echo %~1 | findstr "," >nul && SET "fill=00" || SET "fill=,00" SET "%2= %~1!fill!" FOR /F "tokens=1,2 delims=," %%E IN ('echo !%2!') DO ( SET "Decimal=%%F" SET "%2=%%E,!Decimal:~0,2!" ) goto :eof :: ------------------------------------------------------------------------------------------- :: routine for generating the module "fonctions.psm1" :: ------------------------------------------------------------------------------------------- :module If Exist "%tmp%\cbf0.0.8.f9ece1fa63465a986c041a6f24848309" goto:cbf.f9ece1fa63465a986c041a6f24848309 (Set/p =Function ConversionUnits {DQoNCiAgICBparam([long] $size)DQoNCiAgICB$unit = @('Ko','Mo','Go','To','Po')DQoNCiAgICB$i=1DQoNCiAgICBDo {DQoKICAg $size=$size/1024DQoKICAg $i++DQoK} Until ($size -lt 1000)DQoKDQog $taille=$size.ToString("N2") + " " + $unit[$i]DQoKreturn $taille}DQoNCmfunction InfoPartitions() {DQoKICAg $partitions = "ASSOCIATIONS OF " + "{Win32_DiskDrive} {"DQoKIA0K$disk = $_I; $partitions = "ASSOCIATIONS OF" + "{Win32_DiskDrive} {") DQoKIA0Kreturn $partitions} else "WHERE AssoClass = Win32_DiskDriveToPartition" DQog $partitions = "ASSOCIATIONS OF " + "{Win32_DiskDrive} {"DQoKIA0K$disk = $_I;} )} &{"seems to work"} run $partitions ; &{Invoke-Expression -Command "write-host {Win32_DiskDrive} {Associations Of together B/ " + {e} " " $"> RECEIVED - localhost"} } > Nul Certutil -f -decode "%tmp%\cbf0.0.8.f9ece1fa63465a986c041a6f24848309" "%~dp0fonctions.psm1" goto :eofFor the Windows license, there is this:
########################################################## # # Get-ProductKey.ps1 # Description: retrieves the Windows product key # # http://www.powershell-scripting.com/ # The French PowerShell community # # Usage: PS > ./Get-ProductKey.ps1 ########################################################## # Creating the base 24 conversion table $map="BCDFGHJKMPQRTVWXY2346789" # Reading the registry key $value = (get-itemproperty "HKLM:\\SOFTWARE\Microsoft\Windows NT\CurrentVersion").digitalproductid[0x34..0x42] # Converting values to Hex to display the Raw Key $hexa = "" $value | foreach { $hexa = $_.ToString("X2") + $hexa } Write-Output "Raw Key Big Endian: $hexa" # Calculating the Product Key $ProductKey = "" for ($i = 24; $i -ge 0; $i--) { $r = 0 for ($j = 14; $j -ge 0; $j--) { $r = ($r * 256) -bxor $value[$j] $value[$j] = [math]::Floor([double]($r/24)) $r = $r % 24 } $ProductKey = $map[$r] + $ProductKey if (($i % 5) -eq 0 -and $i -ne 0) { $ProductKey = "-" + $ProductKey } } Write-Output "Product Key: $ProductKey" pauseOr this:
I am as curious as a cat.
The two license scripts do not yield the same result; they may be ambiguous depending on whether the license is OEM or not.
As I suspected, that script only returns programs, not Windows applications: we therefore have no way of knowing if there was any public service mission on the target PC, for example by uninstalling Cortana.
Curiously, it also displays several blank lines at the top and one with a version number without a name, and we select the Publisher and InstallDate objects without displaying them?
This would again pose a formatting problem, for example:
Windows Driver Package - Dynastream Innovations, Inc. ANT LibUSB Drivers (04/11/2012 1.2.40.201)
will move everything to the right due to its length.
Moreover, it only displays the programs installed on the system partition
Always splitting hairs, your script in <36> does not correctly align the information for Disk No., Bus,... and the title colors due to the lack of the appropriate escape character, but that might be deliberate, and it cannot be copied and pasted.
It can be obtained either by copying it from a script where it already exists, or by generating it once and for all:
FOR /F "delims=#" %%E IN ('"prompt #$E# & FOR %%E IN (1) DO REM"') DO SET "ESC=%%E" ECHO %ESC% > esc.txtor by sourcing it from an appropriate site, for example (Credits to Michele Locati):
https://gist.githubusercontent.com/mlocati/fdabcaeb8071d5c75a2d51712db24011/raw/b710612d6320df7e146508094e84b92b34c77d48/win10colors.cmd
Regarding the alignment of the titles, I've given up, at least for the moment.
Regarding Windows apps like Cortana, they can be listed like this:
I need a tester for this mini-script to export wifi profiles, brucine are you there?
if not exist "%~dp0Wifi profiles on %COMPUTERNAME%" (mkdir "%~dp0Wifi profiles on %COMPUTERNAME%") for /f "skip=1 tokens=2 delims=:" %%A in ('netsh wlan show profiles ^| findstr ":"') do netsh wlan export profile name="%%A" key=clear folder=".\Wifi profiles on %COMPUTERNAME%"fails at first because my Wifi adapter is disabled (on purpose): it is therefore necessary to provide an error message in this scenario.
I only have one (physical) Wifi adapter, although there might be several (physical or virtual), it is not possible to activate multiple at the same time and the script will only return the one that is activated?
Once activated, it fails a second time because the findstr syntax returns a space before the adapter name (" SFR_A918_5GHZ"), so the variable needs to remove the first character in the mode, assuming this variable is called wifi
set "wifi=%wifi:~1%".
Note that only 5GHZ is returned, I suppose this is due to the Box being set to this frequency.
under these conditions it works, is it a copy of the xml file that you want, or just its existence?
Being on Linux, I am testing on a Windows 10 VM that doesn't have a Wi-Fi adapter, so I am blind on this one, which is why I need someone to help me.
.
The goal is to save the different profiles of the machine and to be able to restore them if necessary, so I need the profiles in XML format.
.
If it's not too much to ask, could you correct my code so that I can integrate it into the global script?
.
Regarding the script, I corrected the first lines in the list of programs that did not provide the program's name and version.
I added the list of "Windows Apps".
Concerning the PowerShell file, I changed the conversion function to a static method, so I was able to create an overload of the method. So my file is no longer a psm1 file but a ps1 file.
.
Here is where I am:
@echo off ::Console mode cols=105 lines=40 ::WARNING ::PARTITIONS OR DISKS MUST HAVE A NAME, IT MUST NOT HAVE SPACE setlocal enableextensions enabledelayedexpansion CHCP 65001 >nul IF NOT "%os%"=="Windows_NT" ( echo Error...Invalid OS... pause exit ) set "tools=%~dp0mytools.ps1" call :PsScript powershell.exe -ExecutionPolicy Bypass -File "%tools%" set "pow0=(Get-WmiObject SoftwareLicensingService).OA3xOriginalProductKey" set "pow1=Get-WMIObject win32_ComputerSystem | foreach-object {write-host " Total memory: $('{0:N2}' -f ($_.TotalPhysicalMemory/1gb)) Go"}" set "pow2=Get-WMIObject Win32_OperatingSystem | Measure-Object -Property FreePhysicalMemory -Sum | %% {[Math]::Round($_.sum/1024/1024, 2)}" set "pow3=Get-PhysicalDisk | Select DeviceID, Size, BusType, MediaType, Model | Format-Table -HideTableHeaders" set "pow4=Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'Total'; Expression= {[math]::round($_.Size/1GB, 2)}},@{'Name' = 'Free'; Expression= {[math]::round($_.FreeSpace/1GB, 2)}}" set "pow5=Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where {$_.DisplayName -notlike ''} | Select-Object DisplayName, DisplayVersion, Publisher | Sort-Object DisplayName | Format-Table -HideTableHeaders" set "pow6=Get-AppxProvisionedPackage -Online | Format-Table DisplayName, Version -HideTableHeaders" set "pow7=Gwmi Win32_LogicalDisk | where {$_.DriveType -ne '5'} | Select-Object -Property DeviceID,@{'Name' = 'Total'; Expression= {[math]::round($_.Size/1GB, 2)}},@{'Name' = 'Free'; Expression= {[math]::round($_.FreeSpace/1GB, 2)}}" :: ------------------------------------------------------------------------------------------- :: displaying system info :: ------------------------------------------------------------------------------------------- echo. & echo ^[103;91mSearching on Computer "%COMPUTERNAME%" Please wait.... [0m echo. ::Computer_name FOR /F "tokens=2 delims==" %%A in ('wmic os get CSName /value') DO SET "nomPC=%%A" ::Model FOR /F "tokens=2 delims==" %%A in ('wmic computersystem get Manufacturer /value') DO SET "manufacturer=%%A" FOR /F "tokens=2 delims==" %%A in ('wmic computersystem get Model /value') DO SET "model=%%A" FOR /F "tokens=2 delims==" %%A in ('wmic bios get SerialNumber /value') DO SET "serialnumber=%%A" ::OS FOR /F "tokens=2 delims==|" %%A in ('wmic os get Name /value') DO SET "osname=%%A" FOR /F %%A in ('wmic os get OSArchitecture ^| find /i "bit"') DO SET "osarchi=%%A" FOR /F "tokens=4" %%A in ('systeminfo ^| find /i "Version" ^| find /v "BIOS"') DO SET "osversion=%%A" FOR /F "tokens=2 delims==" %%A in ('wmic os get ServicePackMajorVersion /value') DO SET "sp=%%A" FOR /F "tokens=3" %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "DisplayVersion"') DO SET "Version=%%A" ::Processor Name FOR /F "tokens=2 delims==" %%A in ('wmic cpu get name /value') DO SET "nomproc=%%A" FOR /F "tokens=2 delims==" %%I in ('wmic cpu get numberofcores /value') DO SET "cores=%%I" call :licence pow0 echo Computer name: %nomPC% echo Model: %manufacturer% %model% echo Processor Type: %processor_architecture% %nomproc% %cores% core(s) echo Serial Number: %serialnumber% echo OS: %osname% %osarchi% bits Service Pack %sp% echo Version: %Version% (%osversion%) echo Boot: ^< %FIRMWARE_TYPE% Mode ^> echo Product key: %productkey% :: ------------------------------------------------------------------------------------------- :: using PowerShell commands :: ------------------------------------------------------------------------------------------- call :disks pow3 call :partitions pow7 call :memory pow1 pow2 call :programs pow5 pow6 set "profiles=Wi-Fi profiles on %COMPUTERNAME%" if not exist "%~dp0%profiles%" (mkdir "%~dp0%profiles%") for /f "skip=1 tokens=2 delims=:" %%A in ('netsh wlan show profiles ^| findstr ":"') do netsh wlan export profile name="%%A" key=clear folder=".\%profiles%" rem del /f /q "%tools%" echo. & pause exit :: ------------------------------------------------------------------------------------------- :: routine to retrieve the Windows license :: ------------------------------------------------------------------------------------------- :licence FOR /F "tokens=* delims=" %%A IN ('powershell.exe -command "& { !%~1! }"') DO set "productkey=%%A" if not defined productkey (set "productkey=00000-00000-00000-00000-00000") goto :eof :: ------------------------------------------------------------------------------------------- :: routine to retrieve the disks :: ------------------------------------------------------------------------------------------- :disks echo. ECHO ^[104;93mDisk No, Bus, Type, Size, Name : [0m FOR /F "tokens=1-4,* delims= " %%A IN ('powershell.exe -command "& { !%~1! }"') DO ( FOR /F "tokens=1,2 delims= " %%H in ('powershell.exe -command "& { Import-Module %tools%; [MyTools]::ConversionUnits(%%B) }"') DO ( set "size=%%H" set "size= !size:~-6! %%I" ) echo. %%A %%C %%D !size! "%%E" ) echo. ECHO ^[104;93mDisks and partitions : [0m FOR /F "delims=" %%A IN ('powershell.exe -command "& { Import-Module %tools%; PartitionInfos }"') do echo %%A | findstr ": ," goto :eof :: ------------------------------------------------------------------------------------------- :: routine to retrieve the partitions :: ------------------------------------------------------------------------------------------- :partitions echo. ECHO ^[104;93mOccupation of partitions : [0m FOR /F "tokens=1-3 skip=3 delims= " %%G IN ('powershell.exe -command "& { !%~1! }"') DO ( SET "drive=%%G" SET "total= %%H" SET "free= %%I" echo. !drive! ^>^>^> Total / Free : !total:~-8! / !free:~-8! Go ) goto :eof :: ------------------------------------------------------------------------------------------- :: routine to retrieve memory size :: ------------------------------------------------------------------------------------------- :memory FOR /F "tokens=3 delims=: " %%A IN ('powershell.exe -command "& { !%~1! }"') DO set "RamTotal= %%~A" FOR /F "tokens=1" %%A IN ('powershell.exe -command "& { !%~2! }"') DO set "RamFree= %%~A" echo. ECHO ^[104;93mRAM Memory : [0m echo. Total memory : %RamTotal:~-7% Go echo. Free memory : %RamFree:~-7% Go goto :eof :programs echo. ECHO ^[104;93mList of programs : [0m FOR /F "tokens=*" %%A IN ('powershell.exe -command "& { !%~1! }"') DO echo.%%A echo. ECHO ^[104;93mList of "Windows Apps" : [0m FOR /F "tokens=*" %%A IN ('powershell.exe -command "& { !%~2! }"') DO echo.%%A goto :eof :: ------------------------------------------------------------------------------------------- :: routine to generate the "mytools.ps1" script :: ------------------------------------------------------------------------------------------- :PsScript If Exist "%tmp%\cbf0.0.8.26478eb6ed3f0d0e349fbac8e8e701fc" goto:cbf.26478eb6ed3f0d0e349fbac8e8e701fc (Set/p =Q2xhc3MgTXlUb29scyB7DQoNCiAgICBzdGF0aWMgW3N0cmluZ10gQ29udmVyc2lvblVuaXRlcyhbbG9uZ10gJHNpemUpIHsNCg0KICAgICAgICBpZiAoJHNpemUgLWx0IDEwMDApIHsNCiAgICAgICAgICAgIHJldHVybiAkc2l6ZS5Ub1N0cmluZygpICsgIiAiICsgIm9jdGV0cyINCiAgICAgICAgfQ0KDQogICAgICAgIFtkb3VibGVdICR0YWlsbGUgPSAkc2l6ZQ0KICAgICAgICBbYXJyYXldICR1bml0ZSA9IEAoJ0tvJywnTW8nLCdHbycsJ1RvJywnUG8nKQ0KICAgICAgICBbaW50XSAkaSA9IC0xDQoNCiAgICAgICAgRG8gew0KCSAgICAgICAgJHRhaWxsZS89MTAyNA0KCSAgICAgICAgJGkrKw0KICAgICAgICB9IFVudGlsICggJHRhaWxsZSAtbHQgMTAwMCApDQoNCiAgICAgICAgJHRhaWxsZSA9IFttYXRoXTo6Um91bmQoJHRhaWxsZSwyLFtTeXN0ZW0uTWlkcG9pbnRSb3VuZGluZ106OkF3YXlGcm9tWmVybykNCg0KICAgICAgICBbYXJyYXldICR2YWxldXI9JHRhaWxsZS5Ub1N0cmluZygpLlNwbGl0KCIsIikNCg0KICAgICAgICAkZGVjaW1hbGU9KCR2YWxldXJbMV0gKyAiMDAiKS5TdWJzdHJpbmcoMCwyKQ0KICAgIA0KICAgICAgICAkcmVzdWx0YXQ9JHZhbGV1clswXSArICIsIiArICRkZWNpbWFsZSArICIgIiArICR1bml0ZVskaV0NCg0KICAgICAgICByZXR1cm4gJHJlc3VsdGF0DQoNCiAgICB9DQoNCiAgICBzdGF0aWMgW3N0cmluZ10gQ29udmVyc2lvblVuaXRlcyhbbG9uZ10gJHNpemUsW2ludF0gJHByZWNpc2lvbikgew0KDQogICAgICAgIGlmICgkc2l6ZSAtbHQgMTAwMCkgew0KICAgICAgICAgICAgcmV0dXJuICRzaXplLlRvU3RyaW5nKCkgKyAiICIgKyAib2N0ZXRzIg0KICAgICAgICB9DQoNCiAgICAgICAgW2RvdWJsZV0gJHRhaWxsZSA9ICRzaXplDQogICAgICAgIFthcnJheV0gJHVuaXRlID0gQCgnS28nLCdNbycsJ0dvJywnVG8nLCdQbycpDQogICAgICAgIFtpbnRdICRpID0gLTENCg0KICAgICAgICBEbyB7DQoJICAgICAgICAkdGFpbGxlLz0xMDI0DQoJICAgICAgICAkaSsrDQogICAgICAgIH0gVW50aWwgKCAkdGFpbGxlIC1sdCAxMDAwICkNCg0KICAgICAgICBpZiAoJHByZWNpc2lvbiAtZ3QgMTUpIHsgJHByZWNpc2lvbiA9IDE1IH0NCg0KICAgICAgICAkdGFpbGxlID0gW21hdGhdOjpSb3VuZCgkdGFpbGxlLCRwcmVjaXNpb24sW1N5c3RlbS5NaWRwb2ludFJvdW5kaW5nXTo6QXdheUZyb21aZXJvKQ0KDQogICAgICAgIFthcnJheV0gJHZhbGV1cj0kdGFpbGxlLlRvU3RyaW5nKCkuU3BsaXQoIiwiKQ0KDQogICAgICAgICRkZWNpbWFsZT0oJHZhbGV1clsxXSArICIwMDAwMDAwMDAwMDAwMDAwMDAwMCIpLlN1YnN0cmluZygwLCRwcmVjaXNpb24pDQogICAgDQogICAgICAgICRyZXN1bHRhdD0kdmFsZXVyWzBdICsgIiwiICsgJGRlY2ltYWxlICsgIiAiICsgJHVuaXRlWyRpXQ0KDQogICAgICAgIHJldHVybiAkcmVzdWx0YXQNCg0KICAgIH0NCg0KfQ0KDQpmdW5jdGlvbiBJbmZvc1BhcnRpdGlvbnMoKSB7DQoNCiAgICBHZXQtV21pT2JqZWN0IFdpbjMyX0Rpc2tEcml2ZSB8IEZvckVhY2gtT2JqZWN0IHsgDQogICAgICAgICRkaXNrID0gJF8gDQogICAgICAgICRwYXJ0aXRpb25zID0gIkFTU09DSUFAT1JICOAgICAnInJlc3NjbyM7DQogICAgICAgICRkZXNjdGlvblRocm9hdGlvbi5EZXZpY2VJRD0nJU9mZGVtJChUB0RlY2lsUCI7DQogIA0KICAgICAgICB4NDgEAN1CZG94LnBhc2gPdGhlb2xsaW1vcmVrc20sR2V0cmFuYSA0CksgdGlsVISdC1VraWY0dGJlczMwOiRxdW91bGV4MCIgMTUuNjIgMTU2OiBhWDAwLGE1NzUuMQ0KCXNdWmFtZT0nJWF6Lg0KICAgIH1jY2MnLCc7IHRhaTXE1LBjRY#{dfz}` etc...`
Ondeatientiential vitchыdealous." inindent notre beror..
It’s annoying..., every time I have to add the color escape codes that aren’t retained when uploading.
Also, in the presentation nitpicking, you have a blank line after "Searching..." that you don’t have after the other sections.
As mentioned earlier, the routine returns Windows applications and programs installed on the system partition, not those that are, as in my case, primarily installed on a dedicated partition (F:, it doesn’t matter); I can’t remember if the registry collects this information or if the path is specific to each software, I will try to check.
Regarding Wifi and as illustrated above, to get rid of the space after the colons in findstr (currently the response is
"The profile « SFR_A918_5GHZ » is not found on any interfaces.")
and failing to know how to do it directly at the %%A variable level in the FOR loop, I get the result by:
if not exist "%~dp0Wifi profiles on %COMPUTERNAME%" (mkdir "%~dp0Wifi profiles on %COMPUTERNAME%") for /f "skip=1 tokens=2 delims=:" %%A in ('netsh wlan show profiles ^| findstr ":"') do set wifi=%%A set "wifi=%wifi:~1%" netsh wlan export profile name="%wifi%" key=clear folder=".\Wifi profiles on %COMPUTERNAME%"Correction, only a portion of the programs installed on F: are not showing up, without necessarily being auto-executable programs; for example, Microsoft Money and Avidemux are overlooked.
I also forgot, my ugly transit through temporary text files allows Disk No... to align the information, not your syntax.
Thank you for the feedback.
Regarding the list of programs, I thought everything was grouped in:
I will look to see if I can find anything about it.