Powershell Command vs Variable Formatting

Viewed 43

I'm playing around with some Powershell commands and implementing them into scripts.

I noticed something that I cannot find much information on despite looking for a few hours so maybe you can help me here. This could also be lack of me searching for the wrong things, apologies ahead if that is the case.

What I'm experimenting with here is manipulating services through PS. Namely, for now, just getting the TaskName. Here is what I am doing:

PS C:\WINDOWS\system32> Get-ScheduledTask -TaskName 'Adobe*' | Select -ExpandProperty TaskName

Output:
Adobe Acrobat Update Task
Adobe Flash Player NPAPI Notifier
Adobe Uninstaller

This is all well and good. However, if I assign that command to a variable from within a powershell script and run the script:

$TaskNames = Get-ScheduledTask -TaskName 'Adobe*' | Select -ExpandProperty TaskName

Output:
Adobe Acrobat Update Task Adobe Flash Player NPAPI Notifier Adobe Uninstaller

So my questions here are:

  1. Why does the formatting change when assigning the command to a variable and calling that variable as opposed to just writing the command explicitly
  2. How can I get the variable call to format output as if I'm just typing the command in the first example
1 Answers
$TaskNames -join "`n" 

... it's doing this probably because one is output of an object, and one is output of an array. If you do $TaskNames.getType() it should tell you it's an array. -join displays an array joined by whatever character you specify.

Related