[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register 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
?
194 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 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Here's a look at newsworthy articles and community happenings during the last month.
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

650 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