Home > OS >  Powershell Where-Object and BitLocker
Powershell Where-Object and BitLocker

Time:08-31

I've got a powershell script where the end goal is to just print the BitLocker Recovery Key to a text file, but I want to use the Where-Object to get only the RecoveryPassword, as opposed to the TPM information.

If I run the last line, it works fine. If I try to run the command passed to a variable, I get an error stating ".KeyProtectorType : The term '.KeyProtectorType' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

I think it's some issue with directory changing because it's being passed in a variable, but I do not know how to work around it.

$location = "$env:UserProfile\Desktop\BitLockerRecoveryKey.txt"

$args = "Get-BitLockerVolume -MountPoint C:| Select-Object -ExpandProperty KeyProtector | Where-Object{$_.KeyProtectorType -eq 'RecoveryPassword'}| Select-Object KeyProtectorID,RecoveryPassword >"    $location
# Get the ID and security principal of the current user account

iex $args
Get-BitLockerVolume -MountPoint C:| Select-Object -ExpandProperty KeyProtector | Where-Object{$_.KeyProtectorType -eq 'RecoveryPassword'}| Select-Object KeyProtectorID,RecoveryPassword

CodePudding user response:


In your case, you can use the value of variable $location directly to pass the output file path to >, the redirection operator

$location = "$env:UserProfile\Desktop\BitLockerRecoveryKey.txt"

Get-BitLockerVolume -MountPoint C:|
  Select-Object -ExpandProperty KeyProtector |
  Where-Object KeyProtectorType -eq RecoveryPassword | 
  Select-Object KeyProtectorID, RecoveryPassword > $location

Note that I've used simplified syntax in the Where-Object call above.

  • Related