aditya pratti
aditya pratti

Reputation: 35

How to pipe the output of a powershell variable to text file

I have declared a powershell variable to get the packages installed on Windows 2016 server by defining a variable and i want to pipe it to a text file.

$jvn=Get-Command java | Select-Object Version

I have tried using $jvn=Get-Command java | Select-Object Version | Out-File -FilePath .\jvn.txt but this prints on the screen , not in text file , i want the output in the text File as Java Version 8.0.202.26

Upvotes: 0

Views: 4965

Answers (2)

js2010
js2010

Reputation: 27428

gcm java | foreach { $version = $_.version; "Java Version $version" } > jvn.txt

Upvotes: -1

ArcSet
ArcSet

Reputation: 6860

So it sounds based on the comments that the output is happening but you want to change the name of the property from Version to Java Version.

Get-Command java | Select-Object @{N=’Java Version’; E={$_.Version}} | Out-File -FilePath C:\test\jvn.txt

The main difference from what you posted Get-Command java | Select-Object Version | Out-File -FilePath .\jvn.txt to the snippet above is the command Select-Object @{N=’Java Version’; E={$_.Version}}.

So lets break that down. We are creating a hashtable @{}. In the hash table we are adding N="New Name Of Property"; E="Property Value". The N is short for Name and the E is short for Expression.

Upvotes: 2

Related Questions