Perhaps you'd be better off using the Win32_EncryptableVolume Class.
This example will create variables, e.g. %EncryptedDriveLetter[1]%
, %EncryptedDriveLetter[2]%
, %EncryptedDriveLetter[3]%
etc., with the respective content of e.g. C:
, E:
, F:
@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
For /F Delims^== %%G In ('2^> NUL Set EncryptedDriveLetter[')Do Set "%%G="
For /F Delims^= %%G In ('^""%SystemRoot%\System32\wbem\WMIC.exe" ^
/NameSpace:\\root\CIMv2\Security\MicrosoftVolumeEncryption Path ^
Win32_EncryptableVolume Where ^
"ConversionStatus!='0' And EncryptionMethod!='0' And VolumeType<'2'" ^
Get DriveLetter 2^> NUL ^| "%SystemRoot%\System32\find.exe" ":"^"'
)Do (Set /A i+=1 & SetLocal EnableDelayedExpansion
For %%H In (!i!) Do EndLocal & Set "EncryptedDriveLetter[%%H]=%%G")
Set EncryptedDriveLetter[ & Pause
Notes: This must be run 'As administrator', and the last line is included just to provide some visual output. You would of course, replace that, with the rest of your script.
Just to be sure that you understand why I provided this methodology; if you were simply wanting to decrpt the encrypted drives, you don't need a for
loop, or variables or manage-bde
. You would just change the Get
method to Call
and use Decrypt
.
For example:
@"%SystemRoot%\System32\wbem\WMIC.exe" /NameSpace:\\root\CIMv2\Security\MicrosoftVolumeEncryption Path Win32_EncryptableVolume Where "ConversionStatus!='0' And EncryptionMethod=!'0' And VolumeType<'2'" Call Decrypt
Just to mention, if the protection status prior to decryption was 1
, i.e. Protection On, upon successful completion the protection status will be changed to 0
, i.e. Protection Off.