1

In powershell I am trying to do the following:

$name = "computername"
#get installed programs
Write-Host "****APPLICATIONS"
gwmi win32_Product -ComputerName $name | select name

#gets services
write-host "****SERVICES"
Get-Service -ComputerName $name | ft

the expected output would be

****APPLICATIONS
name
of
app

****SERVICES
running services here
more services here

the actual result is

****APPLICATIONS
****SERVICES
name
of
app
running services here
more services here

I have attempted to do start-job then wait-job , but running gwmi as a job seems to output nothing to the console and sending the output to a separate file defeats the purpose of other parts of the script

I also attempted to use start-sleep and it still finishes both write-host commands before proceeding

1
  • While Win32_Product may work it is a generally regarded as a bad way to retrieve information about installed software. Even Microsoft's Hey Scripting Guys have written articles about how bad it is. I would suggest looking at this answer for a faster alternative. Commented Jan 13, 2017 at 0:13

2 Answers 2

1

Try this:

$name = "computername"
Write-Host "`n****APPLICATIONS`n"
gwmi win32_Product -ComputerName $name | % {$_.name}
write-host "`n****SERVICES"
Get-Service -ComputerName $name | ft

If you want the results alphabetical:

$name = "computername"
Write-Host "`n****APPLICATIONS`n"
$apps = gwmi win32_Product -ComputerName $name | % {$_.name}
$apps | sort
write-host "`n****SERVICES"
Get-Service -ComputerName $name | ft
Sign up to request clarification or add additional context in comments.

2 Comments

this works perfectly, could you explain a bit what the `n does here? I've never seen this notation in powershell
@user19702 - That is for newline. It just makes the output easier to read by putting a blank line between the data sets. Without those characters, there would be no space between the Write-Host lines and the applications and services lists. If you want to see the difference, you can remove them and run it.
0
Param(
    $ComputerName = 'AT805061'
)

# Get installed programs
Write-Host "`n****APPLICATIONS`n"
Get-WmiObject win32_Product -ComputerName $ComputerName | Select-Object -ExpandProperty Name | Sort-Object

# Get services
Write-Host "`n****SERVICES`n"
Get-Service -ComputerName $ComputerName | Where-Object -Property Status -eq -Value Running | Select-Object -ExpandProperty Name | Sort-Object

3 Comments

You should try a parameter block instead of a variable. That way you could be like "C:\Scripts\ComputerInfo.ps1 -ComputerName MyComputer1" and it would run the script with that parameter.
Also I believe you just need to modify your script to expand the Name property to strings. They would currently be the type: Selected.System.Management.ManagementObject
Also added sorting and only selecting services that were running.

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.