So to get started, let's talk about the PSCredential object,
which is utilized for storing credentials.
The PSCredential object represents a set of security credentials,
so a username and a password.
Now, to populate the PSCredential object,
we obviously need to provide credentials and then the
Credential object is populated.
Once we have that object, it can be passed as a parameter into other commands.
For example, if we wish to connect to a machine,
we can pass the Credential object as part of that.
Now, to create PSCredential objects,
we can create it using what's referred to as the Get‑Credential cmdlet.
Now, you may have seen this before.
This is where we type Get‑Credential, and it prompts a Windows prompt,
like a regular username,
password Windows prompt that we can enter credentials into,
and then that becomes the basis of the PSCredential object.
The second option is to create a SecureString object.
Now,
what this means is we're basically passing in a kind of
free text string value for the username,
but for the password, we basically type a string text value,
but convert that to a SecureString,
so a different approach to the Get‑Credential.
The last one is to actually just create a PSCredential object, which we can do.
We can say New, PSCredential Object,
and then pass in the various pieces that we need to construct the set of
credentials that we wish to utilize going forward.
Now, if we do this within PowerShell and using that Get‑Credential,
if I wish to create some Globomantics kind of user credentials,
I could create a variable called creds, or credentials,
or whatever you wish to call it, and then just type Get‑Credential.
Now, the difference here, when we just use Get‑Credential with no parameters,
it will launch the Windows box with two empty boxes,
so we'll get the username, and the password will be empty.
Now,
when we pass a Credential property, which will be DOMAIN\user, for example,
then that username field is automatically populated for us.
This is actually quite a common approach that used,
for example, when you connect to Microsoft 365 or
Azure Active Directory in the cloud.
Now, we can create the PSCredential object from the two sets of values.
So we could create a user property.
So you can see, I've got a variable here called user, and it's DOMAIN\user,
and then my password will be ConvertTo‑SecureString, which is the cmdlet
that will change my password to a SecureString value, my password is just
password in blue there, and then of course, as plain text, then I'm going
to save that. To create the PSCredential object, we then create a new
object, and the new object is a System.Management.Automation, and there we
have it, PSCredential, and we pass in the $user and the $pwd as the option.
We can also do it a little bit differently by using properties instead,
so we could define the object with a specific type,
which will still be System.Management.Automation.PSCredential, and
then the argument list or the properties that we wish to send to it would be the $user and the $pwd.