System.Management.Automation.PSCredential & The Dispose Method


Over the last few days, I ended up stealing so much code and I could not know what is what.

Again, trying to solve a bigger problem made my stealing a lot more scattered.


Yet, as the dusk settles, one likely has to go back and understand and may be see what to keep and what to let go off.


One of the things I wanted to do is house keeping.

That is, look through each function, identify the objects instantiated through the new-object invocation and give the resource back to the system through calling the Dispose Method.




# Instantiate new object System.Management.Automation.PSCredential
# from username and password
$credential = new-object System.Management.Automation.PSCredential ($username, $password);

Method – Dispose


#If credential is instantiated, please dispose of it
if ($credential)


Output – Image


Output – Text

Method invocation failed because [System.Management.Automation.PSCredential] does not contain a method named 'Dispose'.


No dispose method.

Get Members ( gm )


#If credential is instantiated, get member
if ($credential)
    $credential | gm;


Output – Image


Output – Text

TypeName: System.Management.Automation.PSCredential

Name                 MemberType Definition
----                 ---------- ----------
Equals               Method     bool Equals(System.Object obj)
GetHashCode          Method     int GetHashCode()
GetNetworkCredential Method     System.Net.NetworkCredential GetNetworkCredential()
GetObjectData        Method     void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void...
GetType              Method     type GetType()
ToString             Method     string ToString()
Password             Property   securestring Password {get;}
UserName             Property   string UserName {get;}


Reviewed the list of members ( Methods and Properties ) and it is obvious that Nah, dispose is not included.


I have a lot more reading to do along the lines of releasing objects whose class does not expose the Dispose object.

Unfortunately, Powershell is a dynamic language and as one does not run the code through a compilation process, some things do not jump out, till when one runs the code.

Or when one goes back, and reviews his or her code.



Listening to D. Nice :-

D-Nice – Call Me D-Nice


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s