Exporting to CSV

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Jason Colotario Jason Colotario 1 year, 2 months ago.

  • Author
  • #27082
    Profile photo of Jason Colotario
    Jason Colotario

    1) I am trying to export the output of the script below to csv. 2) I want to bypass the execution policy.

    My goal is have the person run the script or executable (.ps1 converted to .exe) and have it grab the information and export the data to their root (c:\) drive.

    I've tried looking at other scripts where the export-csv works, but can't seem to nail down the positioning.

    Many thanks in advance


    $ComputerName = $env:COMPUTERNAME,

    begin {
    $RegistryLocation = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\',
    $HashProperty = @{}
    $SelectProperty = @('ProgramName','ProgramVersion','ComputerName')
    if ($Property) {
    $SelectProperty += $Property | export c:\test.csv

    process {
    foreach ($Computer in $ComputerName) {
    $RegBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$Computer)
    foreach ($CurrentReg in $RegistryLocation) {
    if ($RegBase) {
    $CurrentRegKey = $RegBase.OpenSubKey($CurrentReg)
    if ($CurrentRegKey) {
    $CurrentRegKey.GetSubKeyNames() | ForEach-Object {
    if ($Property) {
    foreach ($CurrentProperty in $Property) {
    $HashProperty.$CurrentProperty = ($RegBase.OpenSubKey("$CurrentReg$_")).GetValue($CurrentProperty)
    $HashProperty.ComputerName = $Computer
    $HashProperty.ProgramName = ($DisplayName = ($RegBase.OpenSubKey("$CurrentReg$_")).GetValue('DisplayName'))
    $HashProperty.ProgramVersion = ($DisplayName = ($RegBase.OpenSubKey("$CurrentReg$_")).GetValue('DisplayVersion'))
    if ($DisplayName) {
    New-Object -TypeName PSCustomObject -Property $HashProperty |
    Select-Object -Property

  • #27097
    Profile photo of Valentin Pechersky
    Valentin Pechersky

    As far as I know, "export" is not an alias to "export-csv".

    Also, have you checked if the user has permission to create a file to C:\?

  • #27102
    Profile photo of Jason Colotario
    Jason Colotario

    Hi Valentin,

    I corrected "export" to "export-csv" and then ran the script. It exports a CSV file, however the file does not contain the data that is output to the screen. I've checked and the user has the rights to export to C:\


  • #27103

    I've not tried running your script but at a glance it looks like the problem is that you're exporting the CSV in the BEGIN block.

    The BEGIN block will execute the first time the function is called in the pipeline and that's the only time it will execute.

    You'll need to move the Export-CSV to the PROCESS block but beware that with your current code you'll still get unexpected results because you'll be overwriting the CSV each time the process block runs. i.e. you'll end up with just the data from the last computer you process.

  • #27885
    Profile photo of Jason Colotario
    Jason Colotario

    Hi Matt,

    I really appreciate your feedback. I will definitely keep a mental note of your suggestions. I was in a hurry and decided to use a batch file to get the necessary results. I will just my script and test based on your comments. Subsequently I will reply with my results. I think it should be unnecessary for me to use the batch file anyway.

    Thanks again,


You must be logged in to reply to this topic.