To export the output to a CSV file, where does the piped export command belong within the script?

Hi Expert,

To export the output to a CSV file, where does the piped export command belong? Ultimately I'd like to review the contents of the user accounts within Excel.

# =====================================================================================
# NAME: Ref48Step2GetRemoteUserAccounts.ps1
# Modified: IT Staff
# Date: 03-Feb-2015
# Verison: PowerShell 4.0
# Client OS: Windows 7, Server OS: Windows 2008 R2
# Supply this function for Active Directory user accounts
# Assumes the presence of Microsoft's ActiveDirectory PowerShell module.
# =====================================================================================
  This script can list all of the user accounts.

  This script can list all of the user accounts.
 .PARAMETER  <AccountName>
  Specifies the user account you want to search.
 .PARAMETER      <ComputerName <string[]>
  Specifies the computer(s) on which the command runs. The default is the local computer.
 .PARAMETER  <Credential>
  Specifies a user account that has the permission to perform this action.

  C:\PS> C:\Scripts\Ref48Step2GetRemoteUserAccounts.ps1
  This example shows how to list all the users on the local computer.      
  C:\PS> C:\Script\Ref48Step2GetRemoteUserAccounts.ps1 | Export-CSV -Path "C:\Scripts\RemoteUserAccounts.csv" -NoTypeInformation
  This example will export report to csv file. If you attach the <NoTypeInformation> parameter with command, it will omit the type information
  from the CSV file. By default, the first line of the CSV file contains "#TYPE " followed by the fully-qualified name of the object type.
  C:\PS> C:\Scripts\Ref48Step2GetRemoteUserAccounts.ps1 -ComputerName Computer01 -AccountName "Administrator","Guest"
  This example shows how to list the Administrator and Guest account information on the remote computer.
  C:\PS> $Cre=Get-Credential
  C:\PS> C:\Scripts\Ref48Step2GetRemoteUserAccounts.ps1 -Credential $Cre -Computername "MAPE-tadams"
  This example lists all of the remote user accounts on the newuser computer.

$Obj = @()

Foreach($Computer in $ComputerName)
            $AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
            -Filter "LocalAccount='$True'" -ComputerName $Computer -Credential $Credential -ErrorAction Stop
            $AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
            -Filter "LocalAccount='$True'" -ComputerName $Computer -ErrorAction Stop
      Foreach($LocalAccount in $AllLocalAccounts)
        Set-ExecutionPolicy remotesigned -Force
        Import-Module activedirectory

        $Object = New-Object -TypeName PSObject
            $Object|Add-Member -MemberType NoteProperty -Name "Name" -Value $LocalAccount.Name
            $Object|Add-Member -MemberType NoteProperty -Name "Full Name" -Value $LocalAccount.FullName
            $Object|Add-Member -MemberType NoteProperty -Name "Caption" -Value $LocalAccount.Caption
            $Object|Add-Member -MemberType NoteProperty -Name "Disabled" -Value $LocalAccount.Disabled
          $Object|Add-Member -MemberType NoteProperty -Name "Status" -Value $LocalAccount.Status
          $Object|Add-Member -MemberType NoteProperty -Name "LockOut" -Value $LocalAccount.LockOut
            $Object|Add-Member -MemberType NoteProperty -Name "Password Changeable" -Value $LocalAccount.PasswordChangeable
            $Object|Add-Member -MemberType NoteProperty -Name "Password Expires" -Value $LocalAccount.PasswordExpires
            $Object|Add-Member -MemberType NoteProperty -Name "Password Required" -Value $LocalAccount.PasswordRequired
            $Object|Add-Member -MemberType NoteProperty -Name "SID" -Value $LocalAccount.SID
            $Object|Add-Member -MemberType NoteProperty -Name "SID Type" -Value $LocalAccount.SIDType
            $Object|Add-Member -MemberType NoteProperty -Name "Account Type" -Value $LocalAccount.AccountType
            $Object|Add-Member -MemberType NoteProperty -Name "Domain" -Value $LocalAccount.Domain
            $Object|Add-Member -MemberType NoteProperty -Name "Description" -Value $LocalAccount.Description
            Foreach($Account in $AccountName)
                  $Obj|Where-Object{$psitem.Name -like "$Account"}
Who is Participating?
QlemoConnect With a Mentor Batchelor and DeveloperCommented:
Best to use something like
.\Ref48Step2GetRemoteUserAccounts.ps1 | export-csv -notype file.csv

Open in new window

You can pipe the results to csv file for example..
C:\Scripts\GetRemoteUserAccounts.ps1 -ComputerName Computer01 -AccountName "Administrator","Guest" | Export-CSV -Path "C:\Scripts\RemoteUserAccounts.csv" 

Open in new window

Will SzymkowskiSenior Solution ArchitectCommented:
Try the following....
C:\Scripts\Ref48Step2GetRemoteUserAccounts.ps1 | Export-csv "c:\name.csv" -notypeinformation

CuriousMAUserAuthor Commented:
Thank you, perfectly simple once the answer is clear.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.