how to get all installed software in windows machine using 'Get-ItemProperty' from powershell? - powershell

I tried this in my script.
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall* | Select-Object DisplayName | Format-Table -AutoSize >>as.txt
Output:
'Get-ItemProperty' is not recognized as an internal or external command,
operable program or batch file.

If you have an x64 version of Windows, perhaps something like this single line batch file would help:
#%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile "$MyProgs = Get-ItemProperty 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*'; $MyProgs += Get-ItemProperty 'HKLM:SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'; $MyProgs.DisplayName | Sort-Object -Unique" 1>"as.txt"

Related

How to search a file in resource monitor using powershell script

I would like to know which processes are using a particular file. This needs to be done through a powershell script. I opened resmon.exe using powershell. I would like to search that particular file in the.exe, but through ps script.
Could someone suggest something on this?
You can do this by using the openfiles command in Windows. To enable openfiles to query the local machine, you'll need to run the following command as administrator:
openfiles /local ON
After running that command, you'll need to restart your computer.
Once restarted, you can query your local machine for which process is using a specific file. Here's a snippet of code to get you on the right track:
$OpenFiles = openfiles /query /fo CSV /v | ForEach-Object {$line=$_; if($line -match '","'){$line}} | ConvertFrom-Csv
$OpenFiles | Where-Object -Property 'Open File (Path\executable)' -Like "*FileNameHere.txt*" | ft

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

Update/replace file string on remote computer using PowerShell

I'm trying to update file string on remote computer. In my script i'm using following code.
Get-Content Clients.txt | ForEach-Object \\{ Get-Item "\\$_\D$\Runtime\run.properties" \\} | Replace-FileString.ps1 -Pattern '$PropName=.*' -Replacement '$PropName=$PropValue' -Overwrite
But I'm getting the following error:
Replace-FileString.ps1 : The term 'Replace-FileString.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program
I even tried with Replace-FileString and i'm getting same error.
Is this the correct way or is there any other way to do it?
You specify a script name. PS does not find this script, so insert the path (target system) of your script - e.g. "c:....\Replace-FileString.ps1".
If it's in the same dir then just ".\Replace-FileString.ps1".
I tried the following command and it is working perfectly
Get-Content Clients.txt | ForEach-Object { (Get-Content -Path "\\$_\D$\Runtime\run.properties") -replace "provisioner.events.monitor=.*","provisioner.events.monitor=JagadeeshTest" | Set-Content -Path "\\$_\D$\Runtime\run.properties"}
Replace-FileString.ps1 is part of psappdeploytoolkit and will not be present on a system unless you have specifically installed it.
You can instead use replace (more info on this command) instead:
ForEach ($client in (Get-Content Clients.txt)) {
(Get-Content "\\$client\D$\Runtime\run.properties") -replace "$PropName=.*","$PropName=$PropValue" |
Out-File "\\$client\D$\Runtime\run.properties"
}

Powershell script to fetch task name from notepad and find the status of it in windows task scheduler

I need a PowerShell script where the tasks listed out in a text file needs to be read and the status of those tasks needs to be fetched from the windows task scheduler and output in to a csv file.
I am able to read the contents using Get-Content cmdlet.But Get-ScheduledTask is throwing an error that it is not recognized as a valid cmdlet.
Kindly help me with this as I am new to PowerShell.
Get-ScheduledTask tasks | Get-ScheduledTaskInfo | Select-Object TaskName, LastTaskResult | Export-Csv -NoTypeInformation -Path D:\First\one.csv
I have a txt file named tasks which contain the list of tasks to be iterated and listed out to a csv file
This one works for me:
$tasks = Get-Content -Path "C:\Users\Administrator\Desktop\Tasks.txt"
Get-ScheduledTask -TaskName $tasks | Get-ScheduledTaskInfo | Select-Object TaskName, LastTaskResult | Export-Csv C:\Users\Administrator\Desktop\Tasks.csv -NoTypeInformation
Txt file is in following format:
Task 1
Task 2
Task 3
I've tried it in PS 4 on Windows 8.1, you don't have this cmdlet in previous versions of Windows (like Windows 7 / Windows Server 2008 R2).
For those previous versions, you can use this great module. You should extract the entire folder in your Modules folder, e,g. to C:\Windows\System32\WindowsPowerShell\v1.0\Modules and then you can use Get-ScheduledTask from module like this:
Import-Module TaskScheduler
Get-Content -Path "C:\Users\Administrator\Desktop\Tasks.txt" | ForEach-Object { Get-ScheduledTask -Name $_ | Select-Object Name,LastRunTime } | Export-Csv C:\Users\Administrator\Desktop\Tasks.csv -NoTypeInformation
Other solution would be to try to play around with schtasks.exe or the Schedule.Service COM object but I think the module TaskScheduler is easier for you.

powershell Get-Process with ComputerName is missing Path

I want to get a list of processes under specific folder on some remote machine and kill them. However, if I add -ComputerName, Get-Process does not return Path as desired, thus I cannot Where with Path. Is there a way to Get-Process / Stop-Process on remote machine under a specific path?
// Paths are filled
PS C:\> Get-Process | Format-Table Name, Path
Name Path
---- ----
firefox C:\Program Files (x86)\Mozilla Firefox\firefox.exe
// Paths are empty
PS C:\> Get-Process -ComputerName localhost | Format-Table Name, Path
Name Path
---- ----
firefox
You could use Invoke-Command if Remoting is enabled on the remote server, and perform your action in the scriptblock of the command:
Invoke-Command -ComputerName remoteComputer -Script { param($pathFilter) Get-Process | ?{$_.Path -like $pathFilter} | Format-Table Name, Path } -Args "somefilter*"
I had a similar case that I resolved by using x64 version of powershell.