0

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

ArcSet
  • 6,518
  • 1
  • 20
  • 34
aditya pratti
  • 35
  • 3
  • 11
  • This command you posted `$jvn=Get-Command java | Select-Object Version | Out-File -FilePath .\jvn.txt` is how you write to text file. It will not print to screen if you do this. – ArcSet May 16 '19 at 21:03
  • thanks @ArcSet for your reply , but i want add "Java" to output ,so it displays Java Version ------- 8.0.202.26 – aditya pratti May 16 '19 at 21:35
  • If you want to change the property name to Java Version from Version you can do this. `$jvn=Get-Command java | Select-Object @{N=’Java Version’; E={$_.Version}} | Out-File -FilePath C:\test\jvn.txt` – ArcSet May 16 '19 at 21:37
  • Possible duplicate of [Write output to a text file in PowerShell](https://stackoverflow.com/questions/18469104/write-output-to-a-text-file-in-powershell) – Daniel Mann May 16 '19 at 22:18

2 Answers2

2

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.

ArcSet
  • 6,518
  • 1
  • 20
  • 34
  • Thanks @ArcSet for quick reply, I am new reply to Powershell.i have declared variables like $prm=Get-WmiObject Win32_Product -Computer . -Filter "vendor = 'Wolters Kluwer Financial Services'" | Select-Object Name, Vendor, Version | Out-File -FilePath D:\Software\pkg.txt , this command works fine when run in powershell not as part of script, am I missing something – aditya pratti May 16 '19 at 22:36
-1
gcm java | foreach { $version = $_.version; "Java Version $version" } > jvn.txt
js2010
  • 23,033
  • 6
  • 64
  • 66
  • I have 2 variables$rgteng=Get-WmiObject Win32_Product -Computer . -Filter "vendor = 'WKFS'" | Select-Object Name, Vendor, Version | Out-File -FilePath D:\Software\pkg.txt , $prm=Get-WmiObject Win32_Product -Computer . -Filter "vendor = 'Wolters Kluwer Financial Services'" | Select-Object Name, Vendor, Version | Out-File -FilePath D:\Software\pkg.txt , i want them to write to a same text file , but not happening,when run individually last o/p is print to txt file, any help here – aditya pratti May 17 '19 at 14:56
  • out-file -append – js2010 May 17 '19 at 15:30