Export-CSV

Welcome Forums General PowerShell Q&A Export-CSV

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #171925

    Participant
    Topics: 5
    Replies: 6
    Points: 26
    Rank: Member

    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  🙂

    
    # 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"
    
    

    ·

  • #171937

    Participant
    Topics: 2
    Replies: 483
    Points: 1,151
    Helping Hand
    Rank: Community Hero

    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 } } 🙂

  • #171943

    Participant
    Topics: 5
    Replies: 6
    Points: 26
    Rank: Member

    Thanks, that is exactly what i needed 🙂

  • #172096

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    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 http://www.manning.com. You have already started to access one of the best resources and that is this form. 🙂  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.

You must be logged in to reply to this topic.