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

4
  • 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. Commented May 16, 2019 at 21:03
  • thanks @ArcSet for your reply , but i want add "Java" to output ,so it displays Java Version ------- 8.0.202.26 Commented May 16, 2019 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 Commented May 16, 2019 at 21:37
  • Possible duplicate of Write output to a text file in PowerShell Commented May 16, 2019 at 22:18

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
-1
gcm java | foreach { $version = $_.version; "Java Version $version" } > jvn.txt

2 Comments

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
out-file -append

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.