Import-Counter

Imports performance counter log files and creates the objects that represent each counter sample in the log.
Import-Counter [-Path*] <String[]> [-Counter <String[]>] [-EndTime <DateTime>] [-MaxSamples <Int64>] [-StartTime<DateTime>] [<CommonParameters>]
Import-Counter [-Path*] <String[]> -ListSet* <String[]> [<CommonParameters>]
Import-Counter [-Path*] <String[]> [-Summary] [<CommonParameters>]

The Import-Counter cmdlet imports performance counter data from performance counter log files and creates objects for each counter sample in the file. The PerformanceCounterSampleSet objects that it creates are identical to the objects that Get-Counter returns when it collects performance counter data.

You can import data from comma-separated value (.csv), tab-separated value ( .tsv), and binary performance log (.blg) performance log files. If you are using .blg files, you can import up to 32 files in each command. You can use the parameters of Import-Counter to filter the data that you import.

Along with the Get-Counter and Export-Counter cmdlets, this feature lets you collect, export, import, combine, filter, manipulate, and re-export performance counter data within Windows PowerShell.

Parameters
-Counter <String[]>

  • Default value is All counter
  • Accepts pipeline input ByValue
  • Accepts wildcard characters

Specifies, as a string array, the performance counters. By default, Import-Counter imports all data from all counters in the input files. Enter one or more counter paths. Wildcards are permitted in the Instance part of the path.

Each counter path has the following format. The ComputerName value is required in the path. For instance:

“\<ComputerName><CounterSet>(<Instance>)<CounterName>”

-EndTime <DateTime>

  • Default value is No end time
  • Accepts pipeline input ByValue

Specifies an end date and time that this cmdlet imports counter data between the StartTime and this parameter timestamps. Enter a DateTime object, such as one created by the Get-Date cmdlet. By default, Import-Counter imports all counter data in the files specified by the Path parameter.

-ListSet <String[]>

  • This value is required
  • Accepts pipeline input ByValue
  • Accepts wildcard characters

Specifies the performance counter sets that are represented in the exported files. Commands with this parameter do not import any data.

Enter one or more counter set names. Wildcards are permitted. To get all counter sets in the file, type Import-Counter -ListSet *.

-MaxSamples <Int64>

  • Default value is No maximum
  • Accepts pipeline input ByValue

Specifies the maximum number of samples of each counter to import. By default, Get-Counter imports all of the data in the files specified by the Path parameter.

-Path <String[]>

  • This value is required
  • Accepts pipeline input ByValue
  • Accepts wildcard characters

Specifies the file paths of the files to be imported. This parameter is required.

Enter the path and file name of a, .csv,, .tsv, or .blg file that you exported by using the Export-Counter cmdlet. You can specify only one .csv or .tsv file, but you can specify multiple .blg files (up to 32) in each command. You can also pipe file path strings (in quotation marks) to Import-Counter.

-StartTime <DateTime>

  • Default value is No start time
  • Accepts pipeline input ByValue

Specifies the start date and time in which this cmdlet gets counter data. Enter a DateTime object, such as one created by the Get-Date cmdlet. By default, Import-Counter imports all counter data in the files specified by the Path parameter.

-Summary [<SwitchParameter>]

  • Default value is False

Indicates that this cmdlet gets a summary of the imported data, instead of getting individual counter data samples.

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.

Inputs

System.String

You can pipe performance counter log paths to this cmdlet.

Outputs

Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet,

Microsoft.PowerShell.Commands.GetCounter.CounterSet, Microsoft.PowerShell.Commands.GetCounter.CounterFileInfo This cmdlet returns a Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet. If you use the ListSet parameter, this cmdlet returns a Microsoft.PowerShell.Commands.GetCounter.CounterSet object. If you use the Summary parameter, this cmdlet returns a Microsoft.PowerShell.Commands.GetCounter.CounterFileInfo object.

Examples
  1. Import all counter data from a file:
    PS C:> $Data = Import-Counter -Path ProcessorData.csv
    

    This command imports all counter data from the ProcessorData.csv file into the $Data variable.

  2. Import specific counter data from a file:
    PS C:> $I = Import-Counter -Path "ProcessorData.blg" -Counter "\SERVER01Processor(_Total)Interrupts/sec"
    

    This command imports only the “Processor(_total)Interrupts/sec” counter data from the ProcessorData.blg file into the $I variable.

  3. Select data from a performance counter then export it to a file:
    1. The first command uses Import-Counter to import all of the performance counter data from the ProcessorData.blg files:
      PS C:> $Data = Import-Counter .ProcessorData.blg
      

      The command saves the data in the $Data variable.

    2. The second command displays the counter paths in the $Data variable:
      PS C:> $Data[0].CounterSamples | Format-Table -Property Path
      
         Path
         ----
         \SERVER01Processor(_Total)DPC Rate
         \SERVER01Processor(1)DPC Rate
         \SERVER01Processor(0)DPC Rate
         \SERVER01Processor(_Total)% Idle Time
         \SERVER01Processor(1)% Idle Time
         \SERVER01Processor(0)% Idle Time
         \SERVER01Processor(_Total)% C3 Time
         \SERVER01Processor(1)% C3 Time

      To get the display shown in the command output, the example uses the Format-Table cmdlet to format as a table the counter paths of the first counter in the $Data variable.

    3. The third command gets the counter paths that end in “Interrupts/sec” and saves the paths in the $IntCtrs variable:
      PS C:> $IntCtrs = $Data[0].Countersamples | Where-Object {$_.Path -like "*Interrupts/sec"} | ForEach-Object {$_.Path}
      

      It uses the Where-Object cmdlet to filter the counter paths and the ForEach-Object cmdlet to get only the value of the Path property of each selected path object.

    4. The fourth command displays the selected counter paths in the $IntCtrs variable:
      PS C:> $IntCtrs
      
         \SERVER01Processor(_Total)Interrupts/sec
         \SERVER01Processor(1)Interrupts/sec
         \SERVER01Processor(0)Interrupts/sec
    5. The fifth command uses the Import-Counter cmdlet to import the data:
      PS C:> $I = Import-Counter -Path .ProcessorData.blg -Counter $intCtrs
      

      It uses the $IntCtrs variable as the value of the Counter parameter to import only data for the counter paths in $IntCtrs.

    6. The sixth command uses the Export-Counter cmdlet to export the data to the Interrupts.csv file:
      PS C:> $I | Export-Counter -Path .Interrupts.csv -Format CSV
      

      This example shows how to select data from a performance counter log file (.blg) and then export the selected data to a .csv file. The first four commands get the counter paths from the file and save them in the variable named $Data. The last two commands import selected data and then export only the selected data.

  4. Display all counter paths in a group of imported counter sets:
    1. The first command uses the ListSet parameter of the Import-Counter cmdlet to get all of the counter sets that are represented in a counter data file.:
      PS C:> Import-Counter -Path ProcessorData.csv -ListSet *
      
         CounterSetName     : Processor
         MachineName        : \SERVER01
         CounterSetType     : MultiInstance
         Description        :
         Paths              : {\SERVER01Processor(*)DPC Rate, \SERVER01Processor(*)% Idle Time, \SERVER01
         Processor(*)% C3 Time, \SERVER01Processor(*)% Interrupt Time...}
         PathsWithInstances : {\SERVER01Processor(_Total)DPC Rate, \SERVER01Processor(1)DPC Rate, \SERVER01
         Processor(0)DPC Rate, \SERVER01Processor(_Total)% Idle Time...}
         Counter            : {\SERVER01Processor(*)DPC Rate, \SERVER01Processor(*)% Idle Time, \SERVER01
         Processor(*)% C3 Time, \SERVER01Processor(*)% Interrupt Time...}

      The first command uses the ListSet parameter of the Import-Counter cmdlet to get all of the counter sets that are represented in a counter data file.

    2. The second command gets all of the counter paths from the list set:
      PS C:> Import-Counter -Path ProcessorData.csv -ListSet * | ForEach-Object {$_.Paths}
      
         \SERVER01Processor(*)DPC Rate
         \SERVER01Processor(*)% Idle Time
         \SERVER01Processor(*)% C3 Time
         \SERVER01Processor(*)% Interrupt Time
         \SERVER01Processor(*)% C2 Time
         \SERVER01Processor(*)% User Time
         \SERVER01Processor(*)% C1 Time
         \SERVER01Processor(*)% Processor Time
         \SERVER01Processor(*)C1 Transitions/sec
         \SERVER01Processor(*)% DPC Time
         \SERVER01Processor(*)C2 Transitions/sec
         \SERVER01Processor(*)% Privileged Time
         \SERVER01Processor(*)C3 Transitions/sec
         \SERVER01Processor(*)DPCs Queued/sec
         \SERVER01Processor(*)Interrupts/sec

      This example shows how to display all the counter paths in a group of imported counter sets.

  5. Import counter data from a range of time stamps:
    1. The first command lists in a table the time stamps of all of the data in the ProcessorData.blg file.:
      PS C:> Import-Counter -Path ".disk.blg" | Format-Table -Property Timestamp
      

      The first command lists in a table the time stamps of all of the data in the ProcessorData.blg file.

    2. The second command saves particular time stamps in the $Start and $End variables:
      PS C:> $Start = [datetime]"7/9/2008 3:47:00 PM"; $End = [datetime]"7/9/2008 3:47:59 PM"
      

      The strings are cast to DateTime objects.

    3. The third command uses the Import-Counter cmdlet to get only counter data that has a time stamp between the start and end times (inclusive):
      PS C:> Import-Counter -Path Disk.blg -StartTime $start -EndTime $end
      

      The command uses the StartTime and EndTime parameters of Import-Counter to specify the range.This example imports only the counter data that has a time stamp between the starting an ending ranges specified in the command.

  6. Import a specified number of the oldest samples from a performance counter log file:
    1. The first command uses the Import-Counter cmdlet to import the first (oldest) five samples from the Disk.blg file:
      PS C:> Import-Counter -Path "Disk.blg" -MaxSamples 5
      

      The command uses the MaxSamples parameter to limit the import to five counter samples.

    2. The second command uses array notation and the Windows PowerShell range operator (:
      PS C:> (Import-Counter -Path Disk.blg)[-1 .. -5]
      

      .) to get the last five counter samples from the file. These are the five newest samples.This example shows how to import the five oldest and five newest samples from a performance counter log file.

  7. Get a summary of counter data from a file:
    PS C:> Import-Counter "D:SamplesMemory.blg" -Summary
    
       OldestRecord            NewestRecord            SampleCount
       ------------            ------------            -----------
       7/10/2008 2:59:18 PM    7/10/2008 3:00:27 PM    1000

    This command uses the Summary parameter of the Import-Counter cmdlet to get a summary of the counter data in the Memory.blg file.

  8. Update a performance counter log file:
    1. The first command uses the ListSet parameter of Import-Counter to get the counters in OldData.blg, an existing counter log file:
      PS C:> $Counters = Import-Counter OldData.blg -ListSet * | ForEach-Object {$_.PathsWithInstances}
      

      The command uses a pipeline operator (|) to send the data to a ForEach-Object command that gets only the values of the PathsWithInstances property of each object

    2. The second command gets updated data for the counters in the $Counters variable:
      PS C:> Get-Counter -Counter $Counters -MaxSamples 20 | Export-Counter C:LogsNewData.blg
      

      It uses the Get-Counter cmdlet to get a current sample, and then export the results to the NewData.blg file.This example updates a performance counter log file.

  9. Import performance log data from multiple files and then save it:
    PS C:> $Counters = "D:testpdata.blg", "D:samplesnetlog.blg" | Import-Counter
    

    This command imports performance log data from two logs and saves the data in the $Counters variable. The command uses a pipeline operator to send the performance log paths to Import-Counter, which imports the data from the specified paths.

    Notice that each path is enclosed in quotation marks and that the paths are separated from each other by a comma.

Additional Notes
 This cmdlet does not have a ComputerName parameter. However, if the computer is configured for Windows 
 PowerShell remoting, you can use the Invoke-Command cmdlet to run an Import-Counter command on a remote 
 computer.
Related Links

Export-Counter
Get-Counter
Format-Table
Get-Date