There are multiple types of variables. Variables can contain numbers, strings and more. Sometimes this can be key to how we handle the data on the variable or how we can display or search for information in the variable. First we will take a look at Get-Date. Get-Date pulls your current date and time information from your computer. We can store this information in a variable and then see how that data is being stored/interpreted by PowerShell:
Notice that the type is ‘DateTime’ which is logical considering what could pulled from Get-Date. Now, we can convert the type to a string if we do not need that particular data type. We can accomplish this like so:
Now, let’s take a look at some real world examples. First, we have a query for the Administrator mailbox’s Primary SMTP Address. The value for the PrimarySMTPAddress will be stored in a variable called $SMTP. We then use the ‘.GetType()’ to see what variable type is set with this data storage.
Notice that the type of variable we have now is a very specific ‘SmtpAddress”. What does that mean exactly?
Now, what if we wanted to store all email addresses for a mailbox. The value for that is ‘EmailAddresses’:
Notice that we have a different type (name and base type) for this object’s properties. The name is a ‘ProxyAddressCollection’ because there can be one or more Proxy Addresses (or email addresses) stored in the variable now. Quite useful if we are storing a lot of information.
Next, we want to store the name of a mailbox in a variable called name. We can do so like the example below. Notice that the variable is a string, which makes sense since the value is a name of an object:
We also have numbers, integers, that we can store in variables as well. We may need a count of all items in a mailbox that we aggregate with all other mailboxes for reporting. We can capture this like so (which also shows the value to be an integer]:
Conclusion
So there you have it, it is easy to get the type of a variable with a stored values. Knowing this will help determine if we can add numbers to it, string values, or that It may need to be expanded in order to use the variable / understand its contents.
What Type? GetType()
Categories: