Where can I find PowerShell cmdlet to Object Class documentation?

255 views Asked by At

I am newish to PowerShell, and I have a very Java-centric development background. As such, I lean very heavily on the Javadoc for any API that I am using. I am now trying to write some PowerShell code that is a little more robust than just a simple script. I want to use Try/Catch/Finally, for example, to catch errors and handle them. However, I don't want to just catch the base [system.exception] class.

In Javadoc, if an action throws an Exception, I can see very clearly in the Javadoc what that Exception is, and catch it (in the case where several exceptions may be thrown, and I want to do different things depending on which one it is for example).

I am looking for the analogous documentation for PowerShell. For example,

New-Item F:\ExistingFolder\NewFolder -Type Directory

would throw an exception if NewFolder already exists. It might also throw a different exception if ExistingFolder does not exist.

Where is such documentation if it is available?

1

There are 1 answers

0
Ansgar Wiechers On BEST ANSWER

You'd have to find out the underlying .NET method that's used to do the work and look at its documentation. In case of your example, probably the CreateDirectory method:

Exception                    Condition

IOException                  The directory specified by path is a file.
                             -or-
                             The network name is not known.

UnauthorizedAccessException  The caller does not have the required permission.

ArgumentException            path is a zero-length string, contains only white space,
                             or contains one or more invalid characters. You can query
                             for invalid characters by using the GetInvalidPathChars
                             method.
                             -or-
                             path is prefixed with, or contains, only a colon character
                             (:).

ArgumentNullException        path is null.

PathTooLongException         The specified path, file name, or both exceed the system-
                             defined maximum length. For example, on Windows-based
                             platforms, paths must be less than 248 characters and
                             file names must be less than 260 characters.

DirectoryNotFoundException   The specified path is invalid (for example, it is on an
                             unmapped drive).

NotSupportedException        path contains a colon character (:) that is not part of
                             a drive label ("C:\").

I'm not aware of official (or unofficial) documentation that references the .NET methods called by PowerShell cmdlets.