Export-CSV

Hello,

 

I created the below to get all installed software on Domain PC’s & Servers and then export it to CSV. This part works fine, however my boss now also requires the Computer name name next to each bit of software in the CSV. How would i add another column to the CSV with the the PC name? I also welcome any ideas to make this script better, still learning :slight_smile:

[pre]

Import AD module if it exists. if this errors then you need to install RSAT tolls as a role on the PC/Server you are running this from.

import-module ActiveDirectory

#Get all PCS excluding not Windows PC’s/Servers
$PCS = Get-ADComputer -Filter * | Where{$.Name -NotLike ‘nametoexclude’ -and $.Name -NotLike ‘nametoexclude’} | select -Property Name

#This will propmt the user for credentials, this needs to be domain adminstrator credentials.
$Credentials = Get-Credential

#This part checks if there is directory called C:\Admin\InstalledProgrames and if not creates it so the script does not error.
Write-host “Checking if folder pathname”
$dir = “pathname”
if(!(Test-Path -Path $dir )){
New-Item -ItemType directory -Path $dir
Write-Host “New folder created”
}
else
{
Write-Host “Folder already exists”
}

Foreach ($PC in $PCS)
{
$filename = ‘InstalledProgrammes’
Invoke-Command -ComputerName $PC.Name -Credential $Credentials -ScriptBlock {Get-ItemProperty ‘HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall*’} |
select -Property DisplayName |
WHERE{$.DisplayName -notlike ‘Microsoft Visual*’-and $.DisplayName -notlike ‘Intel*’ -and $.DisplayName -notlike ‘Definition Update*’ -and $.DisplayName -notlike ‘Google Update Helper*’ -and $.DisplayName -notlike ‘I.R.I.S. OCR*’ -and $.DisplayName -notlike ‘Microsoft .NET Framework*’ -and $.DisplayName -notlike ‘Tools for .Net 3.5’ -and $.DisplayName -notlike ‘Microsoft Analysis Services OLE DB Provider*’ -and $.DisplayName -notlike ‘Microsoft Build Tool*’ -and $.DisplayName -notlike ‘Microsoft Help*’ -and $.DisplayName -notlike ‘Microsoft Office OSM*’ -and $.DisplayName -notlike ‘Microsoft Office Proofing*’ -and $.DisplayName -notlike ‘Microsoft Office Shared*’ -and $.DisplayName -notlike ‘SQL Server 2017*’ -and $.DisplayName -notlike ‘Microsoft System CLR*’ -and $.DisplayName -notlike ‘Microsoft VC++’ -and $_.DisplayName -notlike 'Microsoft Visio MUI’ -and $.DisplayName -notlike ‘Outils*’ -and $.DisplayName -notlike ‘Realtek*’ -and $.DisplayName -notlike ‘Roslyn*’ -and $.DisplayName -notlike ‘Security Update*’ -and $.DisplayName -notlike ‘Update for*’ -and $.DisplayName -notlike ‘Blue*’ -and $.DisplayName -notlike ‘Dell*’ -and $.DisplayName -notlike ‘Microsoft Office Shared*’ -and $.DisplayName -notlike ‘Microsoft SQL Server Data-Tier*’ -and $.DisplayName -notlike ‘Microsoft SQL Server 2014 Management*’ -and $.DisplayName -notlike ‘SQL Server 2017 Common*’ -and $.DisplayName -notlike ‘SQL Server 2017 Management Studio Extensions*’ -and $.DisplayName -notlike ‘Active Directory Authentication Library for SQL Server*’ -and $.DisplayName -notlike ‘SSMS Post Install Tasks*’ -and $.DisplayName -notlike ‘Visual Studio*’ -and $.DisplayName -notlike ‘Office 16 Click*’} |
Sort-Object displayname -Unique |
Export-Csv -Append -Path $dir$filename.csv -NoTypeInformation

Invoke-Command -ComputerName $PC.Name -Credential $Credentials -ScriptBlock {Get-ItemProperty ‘HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall*’} |
select -Property DisplayName |
WHERE{$.DisplayName -notlike ‘Microsoft Visual*’-and $.DisplayName -notlike ‘Intel*’ -and $.DisplayName -notlike ‘Definition Update*’ -and $.DisplayName -notlike ‘Google Update Helper*’ -and $.DisplayName -notlike ‘I.R.I.S. OCR*’ -and $.DisplayName -notlike ‘Microsoft .NET Framework*’ -and $.DisplayName -notlike ‘Tools for .Net 3.5*’ -and $.DisplayName -notlike ‘Microsoft Analysis Services OLE DB Provider*’ -and $.DisplayName -notlike ‘Microsoft Build Tool*’ -and $.DisplayName -notlike ‘Microsoft Help*’ -and $.DisplayName -notlike ‘Microsoft Office OSM*’ -and $.DisplayName -notlike ‘Microsoft Office Proofing*’ -and $.DisplayName -notlike ‘Microsoft Office Shared*’ -and $.DisplayName -notlike ‘SQL Server 2017 ’ -and $_.DisplayName -notlike 'Microsoft System CLR’ -and $.DisplayName -notlike ‘Microsoft VC++*’ -and $.DisplayName -notlike ‘Microsoft Visio MUI*’ -and $.DisplayName -notlike ‘Outils*’ -and $.DisplayName -notlike ‘Realtek*’ -and $.DisplayName -notlike ‘Roslyn*’ -and $.DisplayName -notlike ‘Security Update*’ -and $.DisplayName -notlike ‘Update for*’ -and $.DisplayName -notlike ‘Blue*’ -and $.DisplayName -notlike ‘Dell*’ -and $.DisplayName -notlike ‘Microsoft Office Shared*’ -and $.DisplayName -notlike ‘Microsoft SQL Server Data-Tier*’ -and $.DisplayName -notlike ‘Microsoft SQL Server 2014 Management*’ -and $.DisplayName -notlike ‘SQL Server 2017 Common*’ -and $.DisplayName -notlike ‘SQL Server 2017 Management Studio Extensions*’ -and $.DisplayName -notlike ‘Active Directory Authentication Library for SQL Server*’ -and $.DisplayName -notlike ‘SSMS Post Install Tasks*’ -and $.DisplayName -notlike ‘Visual Studio*’ -and $.DisplayName -notlike ‘Office 16 Click*’} |
Sort-Object displayname -Unique |
Export-Csv -Append -Path $dir$filename.csv -NoTypeInformation
Write-Host ‘’$PC.Name’ Completed, moving to next Server’
}

Write-host “Script completed. Please check C:\Admin\InstalledProgrammes for CSV files”

[/pre]
·

When you do Select -Property DisplayName you can also add arbitrary properties as needed, e.g., Select-Object -Property DisplayName, @{ Name = ‘ComputerName’; Expression = { $env:COMPUTERNAME } } :slight_smile:

Thanks, that is exactly what i needed :slight_smile:

Hey Leon,

Depending on the version of PowerShell installed on the remote machines will change how you can do the script. If you are still learning some great books are Learn PowerShell in a Month of Lunches and Learn PowerShell Toolmaking in a Month of Lunches. You can find the books on www.manning.com. You have already started to access one of the best resources and that is this form. :slight_smile: Commonly I code in PowerShell 2.0 when dealing with remote servers because it will work on the majority of the servers even those pesky ones that have not been patched. If your servers are running PSVersion 4.0 you are in a better position. If you would like to discuss the differences in the future let me know happy to have a conversation.