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