As a paranoic man I hide my personal data encrypted. For some time I have been fan of TrueCrypt. It's lightweight, quick, reliable.. simply great. I don't need to install it, so I can have the application stored on my flash drive and the data accessible anytime I wish. The data are kept in a file (of fixed size) that is mapped to a drive after decryption. Then you can work with the data in the same way as you work with a ordinary drive.

To simplify the process you can use a powershell module for mounting and dismounting the drive. First you have to specify the path where TrueCrypt is stored.

$trueCryptpath = 'c:\prgs\truecrypt\truecrypt.exe'
The best way is to specify the variable in your profile file.

After that let's go to put together the two functions for mounting and dismounting the drive. The first function Mount-TrueCryptDrive takes two parameters:

  • $file which points us to the encrypted file
  • $letter which determines the letter drive to mount to (just one letter, e.g. 'x')
  • and optionaly $help to write the help message
function Mount-TrueCryptDrive([string]$file="Your default file",
  [string]$letter="x", [switch]$help) {
  if ($help) {
    write-host "Function to mount TrueCrypt image"
    write-host "-file - path to your file with encrypted image"
    write-host "-letter - disc letter to bind to; default is 'x'"
    Write-Host "to dismount the drive call Dismount-TrueCryptDrive function"
  if ([io.driveinfo]::GetDrives() | ? { $_.Name -match "^$letter" })
      throw "Letter $letter is already mounted"

  if (!(test-path $file)) { throw "file $file not found" }
  $pass = Read-Host 'Specify your password' -AsSecureString
  $marshal = [Runtime.InteropServices.Marshal]
  $pass = $marshal::PtrToStringAuto( $marshal::SecureStringToBSTR($pass) )
  & $trueCryptpath /v $file /l $letter /p $pass /q
  write-host "mounted to $($letter):"
The second function Dismount-TrueCryptDrive is quite simple - it just takes the drive letter ($letter param) to dismount.
function Dismount-TrueCryptDrive(
  [string]$letter=$(throw "you have to specify letter, e.g. x")) {
  & $trueCryptpath /d $letter /q
  write-host "$letter dismounted"
And finally we will set aliases for easier usage.
Set-Alias ttm Mount-TrueCryptDrive -Scope Global
Set-Alias ttd Dismount-TrueCryptDrive -Scope Global

How to use it? Just download the module, change the paths, add module and you are done.

PS C:\ps1> ttm The term 'ttm' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again. At line:1 char:4 + ttm <<<< PS C:\ps1> Add-Module C:\ps1\TrueCrypt.psm1 PS C:\ps1> ttm m:\data\pers2.tuv x Specify your password: **************** mounted to x: PS C:\ps1> dir x:\ Directory: Microsoft.PowerShell.Core\FileSystem::X:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 9.6.2008 8:17 regs -a--- 31.8.2007 16:53 3424 pic1.jpg -a--- 31.8.2007 16:53 3424 pic2.jpg PS C:\ps1> ttd x x dismounted

Download TrueCrypt.psm1

Meta: 2008-08-12, Pepa