I have a Powershell script that technically speaking is working fine but I want to make some things more efficient. In order to export the output to a csv what i usually do is start a powershell and type ./SkriptName.ps1 > OutPutCsvFileName.csv and then proceed doing the other tasks.

I was wondering if there is a way to pipe the output more efficiently. As far as I remember doing Export-CSV and piping it that way didnt work in my previous attempts. I do have a "similar" skript for 2010 although in that one i am creating an array and then outputting it via export-csv.

So if anyone has a suggestion on how to achieve this i'd appreciate it!

$siteurl = "http://sitename/";
$weburl = ""; #leave empty
$listurl = "/Listname/"; #listname
write-host ">>> STARTING at"(get-date) `n;

$site = new-object Microsoft.SharePoint.SPSite("$siteurl");
$web = $site.OpenWeb("$weburl");
$list = $web.GetList("$listurl");

#$ViewFields = '<FieldRef Name="Name" /><FieldRef Name="Sachbearbeiter" /><FieldRef Name="ows_Sachbearbeiter" />'
$query = new-object Microsoft.SharePoint.SPQuery
$query.RowLimit = 1000; # only read 1000 items at a time into memory. Decrease this if you run into memory issues, or increase for a slight speed gain
$query.ViewFields= ""; #or use $ViewFields instead
$query.ViewAttributes += " Scope='Recursive'"; # this is where we make the query disregard folders
$index = 1;

do {
    $itemcoll = $list.GetItems($query);

    foreach ($item in $itemcoll) {

        #write-host $($item.Url) $($item["Sachbearbeiter"]);
        $SB = $item["Val1"];
        $GUID = $item["GUID"];
        write-output "$($item.UniqueId)|$SB|$GUID|$($item.url)";
    $query.ListItemCollectionPosition = $itemcoll.ListItemCollectionPosition;
} while ($query.ListItemCollectionPosition -ne $null);

write-host ">>> FINISHED at"(get-date) `n;

1 Answer 1


You can use write-output instead and redirect to a file ( > OutPutCsvFileName.csv or pipe to out-file OutPutCsvFileName.csv)

  • Won't work unfortunately, if i use "| out-file" after the "}" it will tell me that i cannot pipe empty values or somethign along those lines. If i write | out-file filename.csv after the write-output "$($item....." then it will keep overwriting the file for every time the loop is executed.
    – Orbital
    Jan 8, 2014 at 11:55
  • Nevermind used the -append flag.
    – Orbital
    Jan 8, 2014 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.