Learn how to a build a cloud-first strategyRegister Now

x
?
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
Medium Priority
?
198 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 71

Accepted Solution

by:
Qlemo earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

810 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