Skip to content

Script to Export BitLocker Recovery Keys for Managed Windows Devices to CSV

Export BitLocker Recovery Keys for Managed Windows Devices

Section titled “Export BitLocker Recovery Keys for Managed Windows Devices”

This script connects to the Microsoft Graph API for a specified tenant, retrieves BitLocker recovery keys for all managed Windows devices, and exports them to a CSV file. It fetches details like key IDs, creation dates, and device IDs, and then joins this data with information from managed devices, such as device names, last sync dates, and encryption status. The resulting dataset is exported to a CSV file, making it easy to track and manage BitLocker recovery keys across the tenant’s devices.


Ideal for: IT security administrators managing device encryption and recovery key compliance

FeatureCapabilityBusiness Value
Comprehensive Key ExportRetrieves all BitLocker recovery keysComplete recovery key inventory
Device Information JoinCombines key data with device detailsFull context for key management
CSV ExportStructured data export for reportingEasy integration with compliance tools
Encryption Status TrackingMonitors device encryption stateSecurity compliance monitoring

Terminal window
<#
.SYNOPSIS
Exports BitLocker recovery keys for all managed Windows devices in the specified tenant to a CSV file.
.DESCRIPTION
This function connects to Microsoft Graph **API** using the specified tenant ID and scopes, retrieves BitLocker recovery keys for all managed Windows devices in the tenant, and exports them to a CSV file.
.PARAMETER TenantId
The ID of the tenant to connect to. Default value is "oncld.io".
.PARAMETER FilePath
The path of the CSV file to export the BitLocker recovery keys to. Default value is "$home\download\$TenantId-BitlockerKeys.csv".
.EXAMPLE
Export-BitLockerRecoveryKeys -TenantId "contoso.onmicrosoft.com" -FilePath "C:\temp\BitLockerKeys.csv"
This command exports BitLocker recovery keys for all managed Windows devices in the "contoso.onmicrosoft.com" tenant to a CSV file named "BitLockerKeys.csv" in the "C:\temp" directory.
.NOTES
Requires the Microsoft.Graph.Authentication, Microsoft.Graph.Identity.SignIns, Microsoft.Graph.DeviceManagement, and JoinModule modules.
#>
function Export-BitLockerRecoveryKeys {
    param (
        [string]$TenantId = "xxx.com",
        [string]$FilePath = "$home\Downloads\$TenantId-BitlockerKeys.csv"
    )
    try{
        # Connect to Microsoft Graph **API**
        Connect-MgGraph -TenantId $TenantId -Scopes "BitLockerKey.Read.All", "BitLockerKey.ReadBasic.All","DeviceManagementManagedDevices.Read.All", "DeviceManagementManagedDevices.ReadWrite.All"
        # Retrieve BitLocker recovery keys and managed devices
        $BitLockerRecoveryKeys = Get-MgInformationProtectionBitlockerRecoveryKey -All -Property "id","createdDateTime","DeviceId" | Select-Object @{N="KeyId";E={$_.id}},"createdDateTime",@{N="AzureAdDeviceId";E={$_.DeviceId}},Key
        $BitLockerRecoveryKeys | ForEach-Object {
            $_.Key =  Get-MgInformationProtectionBitlockerRecoveryKey -BitlockerRecoveryKeyId $_.KeyId -Property "Key" | Select-Object -ExpandProperty Key
        }
        $ManagedDevices = Get-MgDeviceManagementManagedDevice -Filter "operatingSystem eq 'Windows'" -Property "DeviceName","Id","AzureAdDeviceId","LastSyncDateTime","IsEncrypted" |
                            Select-Object "DeviceName","Id","AzureAdDeviceId","LastSyncDateTime","IsEncrypted"
        # Join the BitLocker recovery keys and managed devices on AzureAdDeviceId
        $Keys = Join-Object -LeftObject $BitLockerRecoveryKeys -RightObject $ManagedDevices -JoinType Left -On AzureAdDeviceId
  # Export the BitLocker recovery keys to a CSV file
  $Keys | Export-Csv -Path $FilePath -NoTypeInformation
  }
  Catch {
Write-Error $_.Exception.Message
}
}

ComponentRequirementPurpose
Microsoft Graph ModulesMicrosoft.Graph.Authentication, Microsoft.Graph.Identity.SignIns, Microsoft.Graph.DeviceManagement, JoinModuleAPI connectivity and data processing
BitLocker PermissionsBitLockerKey.Read.All, BitLockerKey.ReadBasic.AllAccess recovery key data
Device Management PermissionsDeviceManagementManagedDevices.Read.All, DeviceManagementManagedDevices.ReadWrite.AllAccess device information
Admin RightsIntune Administrator or Global AdministratorSufficient permissions for device data
ParameterDefaultDescription
TenantId"xxx.com"Target tenant for key export
FilePath"$home\Downloads\$TenantId-BitlockerKeys.csv"Output CSV file location

  1. Connect to Microsoft Graph API

    • Establish authenticated connection
    • Verify required permissions
  2. Retrieve BitLocker Recovery Keys

    • Get all recovery keys with metadata
    • Extract key IDs, creation dates, device associations
  3. Fetch Managed Device Information

    • Query Windows devices only
    • Include device name, sync status, encryption state
  4. Data Join and Processing

    • Join keys with device information
    • Create comprehensive dataset
  5. Export to CSV

    • Generate structured CSV output
    • Include all relevant metadata

ColumnDescriptionBusiness Value
KeyIdUnique recovery key identifierKey tracking and reference
createdDateTimeKey creation timestampAge and rotation analysis
AzureAdDeviceIdAssociated device identifierDevice-key relationship
KeyActual recovery key valueEmergency recovery access
DeviceNameHuman-readable device nameUser-friendly identification
LastSyncDateTimeLast device sync timestampData freshness indicator
IsEncryptedDevice encryption statusCompliance monitoring

Terminal window
Export-BitLockerRecoveryKeys -TenantId "contoso.onmicrosoft.com"
Terminal window
Export-BitLockerRecoveryKeys -TenantId "contoso.onmicrosoft.com" -FilePath "C:\Reports\BitLockerKeys.csv"

ScenarioPurposeBenefit
Compliance ReportingDocument recovery key inventoryAudit readiness
Emergency RecoveryAccess keys for device recoveryBusiness continuity
Security AuditingReview encryption coverageRisk assessment
Key ManagementTrack key age and rotationSecurity hygiene
ConsiderationRiskMitigation
Key ExposureSensitive recovery dataSecure CSV storage and access
Data PrivacyDevice and key informationImplement proper access controls
Retention PolicyLong-term key storageDefine appropriate retention periods

Key Takeaway: This script provides comprehensive BitLocker recovery key management capabilities, enabling organizations to maintain proper security compliance, support emergency recovery scenarios, and ensure complete visibility into device encryption status across the enterprise.