Skip to content

Instantly share code, notes, and snippets.

@mirsaeedi
Created November 21, 2017 21:38
Show Gist options
  • Save mirsaeedi/d3ad13bc6d3e44700787718751b1cca6 to your computer and use it in GitHub Desktop.
Save mirsaeedi/d3ad13bc6d3e44700787718751b1cca6 to your computer and use it in GitHub Desktop.
How to unlock BitLocker drives in Windows
using System.Management;
namespace Windows.Security.FileSystem
{
public class BitLocker
{
private readonly string _computerIp;
public BitLocker(string computerIp)
{
_computerIp = computerIp;
}
/// <summary>
/// Simple Usage for localhost:
/// var bitlocker = new BitLocker("localhost");
/// var result = bitlocker.UnlockWithPassphrase("D:", "password");
/// </summary>
/// <param name="driveLetter"></param>
/// <param name="passphrase"></param>
/// <returns></returns>
public object UnlockWithPassphrase(string driveLetter,string passphrase)
{
object result=null;
var wmiNamespace="\\\\{0}\\root\\CIMV2\\Security\\MicrosoftVolumeEncryption";
var scope = new ManagementScope(string.Format(wmiNamespace, _computerIp));
var query = new ObjectQuery("SELECT * FROM Win32_EncryptableVolume");
var searcher = new ManagementObjectSearcher(scope, query);
var allVolumes = searcher.Get();
foreach (ManagementObject volume in allVolumes)
{
if (volume.Properties["DriveLetter"].Value.ToString()==driveLetter)
{
object[] methodArgs = {passphrase};
result = volume.InvokeMethod("UnlockWithPassphrase", methodArgs);
}
}
return result;
}
}
}
@mirsaeedi
Copy link
Author

the class usage is as follows:

var bitlocker = new BitLocker("localhost");
var result = bitlocker.UnlockWithPassphrase("D:", "password");

@serbinskis
Copy link

Note: requires admin rights

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment