After a few months had passed, I struggled because I forgot how to do it, so I decided to take notes again. Information output by PowerShell can be hard to read as-is, but formatting it appropriately makes it easier to read.
Usage Examples
1. Check if Python is installed and output version simultaneously
$var = Get-Command python
$var = $var.Version
"" + $var.Major + '.' + $var.Minor
2. Check if Ruby is installed and output version simultaneously
$var = Get-Command ruby
$var = $var.Version
"" + $var.Major + '.' + $var.Minor
3. Format and output date
$var = Get-Date
$DATE = "" + $var.Year + $var.Month + $var.Day + $var.Hour + $var.Minute + $var.Second
$today = "" + $var.Year + '-' + $var.Month + '-' + $var.Day
echo $var $DATE $today
Output result:
July 13, 2017 2:24:25
201771322421
2017-7-13
Example with pwd
For the pwd command, as a special case, using the variable directly produces similar output.
PS C:\> pwd
Path
----
C:\
PS C:\> pwd.Path
pwd.Path : The term 'pwd.Path' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ pwd.Path
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (pwd.Path:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Using it directly as shown above causes an error, but assigning it to a variable solves the problem.
$var = pwd
$var.Path
Output result:
C:\
Notes
Regarding the special case of the pwd command, it’s convenient to remember that you can get the same output with a variable that can be used directly.
Using the methods above, you can format output in PowerShell into an easy-to-read format. When you’re in trouble, please refer to this.