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
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 70

Accepted Solution

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

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 40587221
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
ID: 40587222
Try the following....
C:\Scripts\Ref48Step2GetRemoteUserAccounts.ps1 | Export-csv "c:\name.csv" -notypeinformation

Will.
0
 

Author Closing Comment

by:CuriousMAUser
ID: 40587229
Thank you, perfectly simple once the answer is clear.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In-place Upgrading Dirsync to Azure AD Connect
This article explains the steps required to use the default Photos screensaver to display branding/corporate images
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

687 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