Solved

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

Posted on 2015-02-03
4
169 Views
Last Modified: 2015-02-03
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
#
# DESCRIPTION:
# Supply this function for Active Directory user accounts
#
# Assumes the presence of Microsoft's ActiveDirectory PowerShell module.
# =====================================================================================
<#
 .SYNOPSIS
  This script can list all of the user accounts.

 .DESCRIPTION
  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.

 .EXAMPLE
  C:\PS> C:\Scripts\Ref48Step2GetRemoteUserAccounts.ps1
  This example shows how to list all the users on the local computer.      
      
 .EXAMPLE
  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.
 
 .EXAMPLE
  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.
 
 .EXAMPLE
  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.
#>

Param
(
      [Parameter(Position=0,Mandatory=$false)]
      [ValidateNotNullorEmpty()]
      [Alias('cn')][String[]]$ComputerName=$Env:COMPUTERNAME,
      [Parameter(Position=1,Mandatory=$false)]
      [Alias('un')][String[]]$AccountName,
      [Parameter(Position=2,Mandatory=$false)]
      [Alias('cred')][System.Management.Automation.PsCredential]$Credential
)
      
$Obj = @()

Foreach($Computer in $ComputerName)
{
      If($Credential)
      {
            $AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
            -Filter "LocalAccount='$True'" -ComputerName $Computer -Credential $Credential -ErrorAction Stop
      }
      else
      {
            $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
            
            $Obj+=$Object
      }
      
      If($AccountName)
      {
            Foreach($Account in $AccountName)
            {
                  $Obj|Where-Object{$psitem.Name -like "$Account"}
            }
      }
      else
      {
            $Obj
      }
}
0
Comment
Question by:CuriousMAUser
4 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
Best to use something like
.\Ref48Step2GetRemoteUserAccounts.ps1 | export-csv -notype file.csv

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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

0
 
LVL 53

Expert Comment

by:Will Szymkowski
Comment Utility
Try the following....
C:\Scripts\Ref48Step2GetRemoteUserAccounts.ps1 | Export-csv "c:\name.csv" -notypeinformation

Will.
0
 

Author Closing Comment

by:CuriousMAUser
Comment Utility
Thank you, perfectly simple once the answer is clear.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

This is a little timesaver I have been using for setting up Microsoft Small Business Server (SBS) in the simplest possible way. It may not be appropriate for every customer. However, when you get a situation where the person who owns the server is i…
Synchronize a new Active Directory domain with an existing Office 365 tenant
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now