Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
cls;
write-host ">>> STARTING at"(get-date) `n;
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint") 

$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;
    $index++;
} while ($query.ListItemCollectionPosition -ne $null);

write-host ">>> FINISHED at"(get-date) `n;
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
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 at 11:55
    
Nevermind used the -append flag. –  Orbital Jan 8 at 11:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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