Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reference PSCredential.Empty in powershell script

I am trying to use empty credentials for anonymous access. How to I set a variable to empty credentials. I am trying to use PSCredential.Empty Property

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Santiago Squarzon has provided the solution:

[pscredential]::Empty

is the PowerShell equivalent of C#’s PSCredential.Empty, i.e. access to the static Empty property of the System.Management.Automation.PSCredential type.


PowerShell’s syntax for accessing .NET types and their – instance or static – member:

  • In order to refer to a .NET type by its literal name in PowerShell, you must specify it as a type literal, which is the type name enclosed in [...]

    • [pscredential] works as-is, even in the absence of a
      using namespace System.Management.Automation statement, because it happens to be a type accelerator, i.e. a frequently used type that you can access by a short, unqualified name (which can, but needn’t be the same name as that of the type it refererences; e.g., [xml] is short for System.Xml.XmlDocument).

    • Generally, you either need a using namespace statement if you want to use the unqualified name only, or you need to use the namespace-qualified type name; e.g., [System.Management.Automation.PSCredential] or – given that the System. component is optional – [Management.Automation.PSCredential]

    • For a complete discussion of PowerShell’s type literals, see this answer.

  • While instance members of .NET types are accessed with . (member-access operator), just like in C#, accessing static members requires a different operator: ::, the static member-access operator

    • A distinct operator is necessary, because type literals are objects themselves, namely instances of System.Reflection.TypeInfo, so that using . access the latter’s instance properties; e.g. [pscredential].FullName returns the type’s namespace-qualified name.

Also note that, in keeping with PowerShell’s general case-insensitive nature, neither type literals nor member names need be specified case-exactly.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading