This really boils down to a philosophical difference and the different between writing a script and working in the console.
Run the following in the ISE and you will get a similar result as PowerShell Studio:
- Get Process in Variable ISE.png (16.61 KiB) Viewed 33015 times
Would you agree that they are the same thing? If so, then why does the ISE produce two different results?
Since the command hasn’t ran, the ISE will present all the possibilities at once. Meaning, it merges all the possible types the command can return and product a list of all the properties and methods from all the types at once.
This can be confusing and can result in an error prone script. If you do not know the exact type and properties, you could potentially reference the property of one type, yet when the script runs, it can return a different type that doesn’t contain that property.
Referencing the member of the contents of an array (V3 feature), can also lead to error prone scripts.
Let’s look at the following example:
$process = Get-Process -Name Notepad
$nameOfProcess = $process.Name
$nameOfProcess.GetType().Name
$nameOfProcess
Looking at the script, you might assume that $nameOfProcess a string containing “Notepad”. But you would be incorrect if there are multiple instances of Notepad running.
Single Instance Output:
Vs Multiple Instance Output:
As you can see, you are dealing with two different types. One is a single string and the other an array. If the user is not aware of this, they can run into unforeseen problems in a script vs running individual commands in a console.
Note: If you are using PowerShell V2, the multiple instance example would have produced an error.
PowerShell does a lot of conversions under the hood to help make it easier to use in the console, but the side effect is that scripts are more confusing and less predictable.
When writing a script, the types should be clearly defined to help with clarity and readability. When writing a script, we should think about the next person who need to read the script and figure out what is happening. I know we don’t always do this, but that is why it is considered a best practice to avoid aliases (especially custom alias that may only exist on your machine).