title: Implement PowerShell PSProvider *in* PowerShell
body: <p>I'm looking to implement a PowerShell Provider <strong>in</strong> PowerShell.  </p>
  
  <p>I keep thinking that if I just define the types, then import them into my session (import-module), I should be able to have them available.</p>
  
  <p>For example, this <strong>does not work</strong> but its along the path of what I'd like to implement.</p>
  
  <p>I'm obviously missing quite a bit...anyone know if this is possible?</p>
  
  <pre><code># EnvironmentProvider.ps1
      $reference_assemblies = (
  
        "System.Management.Automation, Version=1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      #  "System.Configuration.Install, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
      )
  
      $source = @"
  
      namespace Providers
      {
  
      using System.Management.Automation;
      using System.Management.Automation.Provider;
  
  
          [CmdletProvider("Environments", ProviderCapabilities.None)]
          public class EnvironmentProvider : DriveCmdletProvider
          {
              protected override PSDriveInfo NewDrive(PSDriveInfo drive)
              {
                  return new EnvironmentDriveInfo(drive);
              }
  
              protected override object NewDriveDynamicParameters()
              {
                  return base.NewDriveDynamicParameters();
              }
  
          }
  
           public class EnvironmentDriveInfo : PSDriveInfo
          {
              public EnvironmentDriveInfo(PSDriveInfo driveInfo) : base(driveInfo)
              {
              }
          }
  
  
      }
      "@
  
      # -ea silentlycontinue in case its already loaded
      #
      add-type -referencedassemblies $referenced_assemblies -typedefinition $source -language CSharp -erroraction silentlycontinue
  </code></pre>
  
  <p>After import-module, I try to create the drive "environments":</p>
  
  <pre><code>new-psdrive -psprovider Environments -name "Environments" -root ""
  </code></pre>
  
  <p>errors with:</p>
  
  <pre><code>New-PSDrive : Cannot find a provider with the name 'Environments'.
  </code></pre>
  
  <p>Assuming the provider actually worked, maybe have it return a list of environments: dev, qa, staging, production.</p>
  
  <p>Then I'd like to be able to re-use this through:</p>
  
  <pre><code>c:\adminlib&gt;import-module .\EnvironmentProvider.ps1
  c:\adminlib&gt;environments:
  
  environments:&gt;ls
  dev
  qa
  staging
  production
  
  environments:&gt; cd production
  environments\production&gt; [execute actions against production]
  
  environments\production:&gt; cd dev
  environments\dev:&gt; [execute actions against dev, etc]
  </code></pre>