Objects and types have members such as methods and properties, but only a fraction is publicly visible and usable. There are numerous hidden (private) members. While it is not wise to use these in production code as they can change without notice with new versions, it is a highly interesting playground for advanced PowerShellers.

There is a free PowerShell module called ImpliedReflection which makes private members visible, even in the IntelliSense produced by PowerShell ISE and Visual Studio Code, and you can actually run the members.

For example, the following public type exposes only one public method that you can use to construct PowerShell's module path:

 
PS> [System.Management.Automation.ModuleIntrinsics]::GetModulePath

OverloadDefinitions                                                                                     
-------------------                                                                                     
static string GetModulePath(string currentProcessModulePath, string hklmMachineModulePath, string       
hkcuUserModulePath)                                                                                     
                                                                                                        



PS> [System.Management.Automation.ModuleIntrinsics] | Get-Member -Static


   TypeName: System.Management.Automation.ModuleIntrinsics

Name            MemberType Definition                                                                   
----            ---------- ----------                                                                   
Equals          Method     static bool Equals(System.Object objA, System.Object objB)                   
GetModulePath   Method     static string GetModulePath(string currentProcessModulePath, string hklmMa...
ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System.Object objB)
 

Now let's install ImpliedReflection like this:

Install-Module -Name ImpliedReflection -Scope CurrentUser

Once the module is installed, you need to enable the extension first:

 
PS> Enable-ImpliedReflection -Force 
 

Now, when you revisit the type and look at its members, it still shows only the public members. The extension kicks in once you output the type interactively:

 

PS> [System.Management.Automation.ModuleIntrinsics] | Get-Member -Static


   TypeName: System.Management.Automation.ModuleIntrinsics

Name            MemberType Definition                                                                   
----            ---------- ----------                                                                   
Equals          Method     static bool Equals(System.Object objA, System.Object objB)                   
GetModulePath   Method     static string GetModulePath(string currentProcessModulePath, string hklmMa...
ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System.Object objB)          



PS> [System.Management.Automation.ModuleIntrinsics]

IsPublic IsSerial Name                                     BaseType                                     
-------- -------- ----                                     --------                                     
True     False    ModuleIntrinsics                         System.Object                                



PS> [System.Management.Automation.ModuleIntrinsics] | Get-Member -Static


   TypeName: System.Management.Automation.ModuleIntrinsics

Name                           MemberType Definition                                                    
----                           ---------- ----------                                                    
AddToPath                      Method     static string AddToPath(string basePath, string pathToAdd, ...
CombineSystemModulePaths       Method     static string CombineSystemModulePaths()                      
Equals                         Method     static bool Equals(System.Object objA, System.Object objB)    
ExportModuleMembers            Method     static void ExportModuleMembers(System.Management.Automatio...
GetDscModulePath               Method     static string GetDscModulePath()                              
GetExpandedEnvironmentVariable Method     static string GetExpandedEnvironmentVariable(string name, S...
GetManifestGuid                Method     static guid GetManifestGuid(string manifestPath)              
GetManifestModuleVersion       Method     static version GetManifestModuleVersion(string manifestPath)  
GetModuleName                  Method     static string GetModuleName(string path)                      
GetModulePath                  Method     static string GetModulePath(string currentProcessModulePath...
GetPersonalModulePath          Method     static string GetPersonalModulePath()                         
GetSystemwideModulePath        Method     static string GetSystemwideModulePath()                       
IsModuleMatchingModuleSpec     Method     static bool IsModuleMatchingModuleSpec(psmoduleinfo moduleI...
IsPowerShellModuleExtension    Method     static bool IsPowerShellModuleExtension(string extension)     
NewAliasInfo                   Method     static System.Management.Automation.AliasInfo NewAliasInfo(...
PathContainsSubstring          Method     static int PathContainsSubstring(string pathToScan, string ...
PatternContainsWildcard        Method     static bool PatternContainsWildcard(System.Collections.Gene...
ProcessOneModulePath           Method     static string ProcessOneModulePath(System.Management.Automa...
ReferenceEquals                Method     static bool ReferenceEquals(System.Object objA, System.Obje...
RemoveNestedModuleFunctions    Method     static void RemoveNestedModuleFunctions(psmoduleinfo module)  
SetModulePath                  Method     static string SetModulePath()                                 
SortAndRemoveDuplicates        Method     static void SortAndRemoveDuplicates[T](System.Collections.G...
_ctor                          Method     static System.Management.Automation.ModuleIntrinsics _ctor(...
MaxModuleNestingDepth          Property   static int MaxModuleNestingDepth {get;}                       
PSModuleExtensions             Property   static string[] PSModuleExtensions {get;set;}                 
PSModuleProcessableExtensions  Property   static string[] PSModuleProcessableExtensions {get;set;}      
SystemWideModulePath           Property   static string SystemWideModulePath {get;set;}                 
Tracer                         Property   static System.Management.Automation.PSTraceSource Tracer {g... 
 

Now you can use private members just as if they were public:

 
PS> [System.Management.Automation.ModuleIntrinsics]::GetPersonalModulePath()
C:\Users\tobwe\Documents\WindowsPowerShell\Modules

PS> [System.Management.Automation.ModuleIntrinsics]::SystemWideModulePath
c:\windows\system32\windowspowershell\v1.0\Modules
 

Again, this is for advanced users who would like to better understand how objects and types work from the inside. The ImpliedReflection module is for playing with private members. You should be very careful though to actually use private members in your production code.

Twitter This Tip! ReTweet this Tip!

Anonymous