Below I have some code to get the values of instances of performance counters (which are instantiated once a page is visited) and send them to Graphite to display graphs in the following format:

`[Path in Graphite (e.g., metric.pages.Counter1)] [value of counter] [epoch time]`

To do this I made the following code where the `writer` is configured correctly to work:

```
# Get all paths to MultipleInstance counters and averages that start with "BLABLA" and 
# put them into an array and get the epoch time 
$pathsWithInstances = (get-counter -ListSet BLABLA*) | select -ExpandProperty PathsWithInstances
$epochtime = [int][double]::Parse((Get-Date -UFormat %s))

# This functions splits the path (e.g., \BLABLA Web(welcome)\Page Requests) into three 
# parts: the part before the 
# opening brace (the CounterCategory, e.g., "\BLABLA Web"), the part in between the braces 
# (the page or 
# service, e.g., "welcome"), and the part after the closing brace (the name of the test, 
# e.g., 
# "\Page Requests"). We obtain the metric out of this information and send it to 
# Graphite.
enter code here
foreach ($pathWithInstance in $pathsWithInstances)
{   
    $instanceProperties = $pathWithInstance.Split('()')
    $counterCategory = $instanceProperties[0]

    if ($counterCategory -eq ("\BLABLA Web") )
    {
        # Replace the * with nothing so that counters that are used to display the 
        # average (e.g., \BLABLAWeb(*)\Page Requests) are displayed on top in the  
        # Graphite directory.

        $pagePath = $instanceProperties[1].Replace('*','')
        $nameOfTheTest = $instanceProperties[2]

        # Countername which is used in Graphite path gets whitespace and backslash 
        # removed in the name used for the path in Graphite (naming conventions)
        $counterName = $nameOfTheTest.Replace(' ','').Replace('\','')
        $pathToPerfCounter = $pathWithInstance
        $pathInGraphite = "metrics.Pages." + $pagePath + $counterName

        #Invoked like this since otherwise the get-counter [path] does not seem to work
       $metricValue = [int] ((Get-Counter "$pathToPerfCounter").countersamples | select -
             property cookedvalue).cookedvalue           
        $metric = ($pathInGraphite + " " + $metricValue +  " " + $epochTime)

        $writer.WriteLine($metric) 
        $writer.Flush() 

    }

}

```

Unfortunately this code is very slow. It takes about one second for every counter to send a value. Does someone see why it is so slow and how it can be improved?