none
PowerShell Script: Export to csv file RRS feed

  • Question

  • OK, first of, a lot of y'all have been helping and guiding me with my first few PowerShell scripts and very thankful and grateful for all of the help. Still learning, of course.

    Now, more process and script command learning needed.

    I started this and everything worked with the exception of the last export to the second csv file. Now, it would be awesome if y'all can point me to right process to get all that into one csv and not 2 separate ones. :)

    Objective: to check and export current registry value and then to set the item property - value and then export the changed value into csv file for verification and confirmation purposes.

    Challenge (Problem): empty csv file after registry key value updated/ changed

    what I have...

    #Get computers from text file. 1 compute per line
    $myComputerList = Get-Content C:\LocalFiles\TEST\ModifyRegistryValue\computerList.txt
    
    #Loop Through Array
    ForEach ($computer in $myComputerList) {
    
    #Execute a command on the computer
    Invoke-Command -ComputerName $computer -ScriptBlock {
    
    #Verify current IM Provider Value for each machine and export it to csv file
    Get-ItemProperty -Path "HKCU:\Software\IM Providers\" |
        Select-Object DefaultIMApp
      } | export-csv C:\LocalFiles\TEST\ModifyRegistryValue\CurrentRegValue.csv
    }
    
    Start-Sleep -s 10
    
    #Get computers from text file. 1 computer per line
    $myComputerList = Get-Content C:\LocalFiles\TEST\ModifyRegistryValue\computerList.txt
    
    #Loop Through Array
    ForEach ($computer in $myComputerList) {
    
    #Execute a command on the computer
    Invoke-Command -ComputerName $computer -ScriptBlock {
    
    #The this command is to change the registry value for each machine and export to CSV file.
        Set-ItemProperty -Path "HKCU:\Software\IM Providers\" -Name "DefaultIMApp" -Value "Lync"} | export-csv -Path C:\LocalFiles\TEST\ModifyRegistryValue\UpdatedRegValue.csv
    }

    Like I've mentioned, I've gotten everything to work, just the way I wanted it, but the last piece is what I am struggling with. It creates the UpdatedRegValue.csv csv file but no information in the csv file - empty file.

    It may be extremely simple for well-versed dynamic folks like y'all but for this newbie? LOL


    TX



    • Edited by GenXit Wednesday, November 13, 2019 5:01 PM adding csv name
    Wednesday, November 13, 2019 4:43 PM

Answers

  • This is how to do this minus the logic, syntax and design errors:

    $computers = Get-Content C:\LocalFiles\TEST\ModifyRegistryValue\computerList.txt
    $sb = {
        #Verify current IM Provider Value for each machine and export it to csv file
        Get-ItemProperty -Path 'HKCU:\Software\IM Providers' |
            Select-Object DefaultIMApp | 
            export-csv C:\LocalFiles\TEST\ModifyRegistryValue\CurrentRegValue.csv
        
        #change the registry value for each machine and export to CSV file.
        Set-ItemProperty -Path 'HKCU:\Software\IM Providers' -Name DefaultIMApp -Value Lync| 
            export-csv -Path C:\LocalFiles\TEST\ModifyRegistryValue\UpdatedRegValue.csv
    }
    Invoke-Command -ComputerName $computers -ScriptBlock $sb1 


    \_(ツ)_/

    From my chicken scratch to this.. amazing! Appreciate you. Will keep hitting the book and the guidance you posted here.

    TX


    • Edited by GenXit Wednesday, November 13, 2019 6:49 PM
    • Marked as answer by GenXit Thursday, December 12, 2019 6:56 AM
    Wednesday, November 13, 2019 6:49 PM

All replies

  • If you formatted your code correctly the cause of the error would be easier to find. If you have no output then it is likely your commands are not written correctly. Also the file will be created remotely.

    Start by learning how to structure code for reliability. You will also have to learn how to handle errors and commands that return no results.

    Your code has many serious syntax issues.  Build and test the ScriptBlock locally before trying to run it remotely.  Correct indentation will help you to find your syntax errors.

    Start here: The PowerShell Best Practices and Style Guide


    \_(ツ)_/


    • Edited by jrvModerator Wednesday, November 13, 2019 6:37 PM
    Wednesday, November 13, 2019 6:32 PM
    Moderator
  • Thanks, jrv

    TX


    • Edited by GenXit Wednesday, November 13, 2019 6:39 PM
    Wednesday, November 13, 2019 6:39 PM
  • This is how to do this minus the logic, syntax and design errors:

    $computers = Get-Content C:\LocalFiles\TEST\ModifyRegistryValue\computerList.txt
    $sb = {
        #Verify current IM Provider Value for each machine and export it to csv file
        Get-ItemProperty -Path 'HKCU:\Software\IM Providers' |
            Select-Object DefaultIMApp | 
            export-csv C:\LocalFiles\TEST\ModifyRegistryValue\CurrentRegValue.csv
        
        #change the registry value for each machine and export to CSV file.
        Set-ItemProperty -Path 'HKCU:\Software\IM Providers' -Name DefaultIMApp -Value Lync| 
            export-csv -Path C:\LocalFiles\TEST\ModifyRegistryValue\UpdatedRegValue.csv
    }
    Invoke-Command -ComputerName $computers -ScriptBlock $sb1 
    


    \_(ツ)_/

    Wednesday, November 13, 2019 6:45 PM
    Moderator
  • I'm reading through the resource you posted here, jrv. Thank you for your expert guidance. Looking to self-learn and from you all.

    TX


    • Edited by GenXit Wednesday, November 13, 2019 6:48 PM
    Wednesday, November 13, 2019 6:48 PM
  • This is how to do this minus the logic, syntax and design errors:

    $computers = Get-Content C:\LocalFiles\TEST\ModifyRegistryValue\computerList.txt
    $sb = {
        #Verify current IM Provider Value for each machine and export it to csv file
        Get-ItemProperty -Path 'HKCU:\Software\IM Providers' |
            Select-Object DefaultIMApp | 
            export-csv C:\LocalFiles\TEST\ModifyRegistryValue\CurrentRegValue.csv
        
        #change the registry value for each machine and export to CSV file.
        Set-ItemProperty -Path 'HKCU:\Software\IM Providers' -Name DefaultIMApp -Value Lync| 
            export-csv -Path C:\LocalFiles\TEST\ModifyRegistryValue\UpdatedRegValue.csv
    }
    Invoke-Command -ComputerName $computers -ScriptBlock $sb1 


    \_(ツ)_/

    From my chicken scratch to this.. amazing! Appreciate you. Will keep hitting the book and the guidance you posted here.

    TX


    • Edited by GenXit Wednesday, November 13, 2019 6:49 PM
    • Marked as answer by GenXit Thursday, December 12, 2019 6:56 AM
    Wednesday, November 13, 2019 6:49 PM
  • I'm reading through the resource you posted here, jrv. Thank you for your expert guidance. Looking to self-learn and from you all.

    TX


    You cannot learn PowerShell by posting in a forum. You must first study PowerShell with a book or by taking a good tutorial. After that then forums will be helpful for specific issues and you will know what to ask about and how to ask a technical question.

    \_(ツ)_/

    Wednesday, November 13, 2019 6:50 PM
    Moderator