SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What would be powershell version of this c# code? I am having hard time get this work in Powershell especially stringbuilder and files.add line where utf8encoding is. Here is my powershell version of the code. Here is the error.

Missing expression after ','.
At C:\PowerShell\Add_Item.ps1:60 char:73
+         $newFile = $docLibrary.RootFolder.Files.Add($newDestinationFolderPath, <<<<
UTF8Encoding.UTF8.GetBytes(builder.ToString()), $true)
    + CategoryInfo          : ParserError: (,:String) [], ParseException
    + FullyQualifiedErrorId : MissingExpressionAfterToken
using ( SPSite siteCollection = new SPSite( "" ) ) {
    using ( SPWeb web = siteCollection.OpenWeb( "docs" ) ) {
        SPList list = web.Lists["Sample"];

        //link to the file
        string fileLinkUrl = "";

        StringBuilder builder = new StringBuilder();

        using ( TextReader reader = new StreamReader( @"C:\linktodocumenttemplate.txt" ) ) {
            builder.Append( reader.ReadToEnd() );

        //replace string template with values
        builder.Replace( "{0}", fileLinkUrl );

        //should change the name of the .aspx file per item
        SPFile file = list.RootFolder.Files.Add( "link_title.aspx", UTF8Encoding.UTF8.GetBytes(builder.ToString()));

        //set list item properties
        SPListItem item = file.Item;
        item["Content Type"] = "Link to a Document";
        SPFieldUrlValue itemUrl = new SPFieldUrlValue();
        itemUrl.Description = "From sample code";
        itemUrl.Url = fileLinkUrl;
        item["URL"] = itemUrl;
        //persist changes
share|improve this question

The builder part you can implement using

 $content = (Get-Content 'C:\linktodocumenttemplate.txt') -replace '\{0}', $filelinkurl

The UTF8 part you can implement using

$utf8 = New-Object -Type "System.Text.UTF8Encoding"
$bytes = $utf8.GetBytes($content)
share|improve this answer

Edit: What error message you get while running your C# code? I have tested this sample code and it works fine for me.

In any case, you can Run/test your C# code using PowerShell, which means your existing C# code could be reused inside Powershell without a need to implement it as Cmdlet.

Technet sample:

share|improve this answer
Thanks. Let me try that.… – Monica Lewinsky Jul 2 '12 at 20:20
I already converted the code into powershell but having trouble files.add line. I think i need to convert utf8 ... to ps friendly. – Monica Lewinsky Jul 2 '12 at 20:35
it seems that you have copied the code from…. Have your created a site and document library as suggested in the code, or changed your parameters? – Falak Mahmood Jul 2 '12 at 20:36
Could you please post the error message? – Falak Mahmood Jul 2 '12 at 20:37
I uploaded the code in a txt file. – Monica Lewinsky Jul 2 '12 at 22:19

This may work:


$oFile = $DocLibrary.RootFolder.Files.Add("TestDoc1.aspx", oWeb.GetFile(oContentType .DocumentTemplateUrl).OpenBinary(), true);
share|improve this answer
I am getting error on $eb.GetFile Exception calling "GetFile" with "1" argument(s): "Value does not fall within the expected range." At C:\PowerShell\Add_Item.ps1:65 char:86 + $file = $spLibrary.RootFolder.Files.Add($filewithpath, $web.GetFile <<<< ($ct.DocumentT emplateUrl).OpenBinary(), $true) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException – Tom Cruise Katy Holmes Jul 3 '12 at 2:11
I put the whole path to getfile method get one more error message in addition to the above error. What the Hell? Microsoft has NO good documentation on this. Exception calling "OpenBinary" with "0" argument(s): "Cannot open file "_layout/newlink.aspx"." At C:\PS\Add_Item.ps1:70 char:40 + $binFile = $spf.OpenBinary <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException – Tom Cruise Katy Holmes Jul 3 '12 at 3:49
This works with some caveat. I downloaded the newlink.aspx file then uploaded to a document library say "templates". then it works. But iconUrl is not same when loading files. – Monica Lewinsky Jul 3 '12 at 17:39

Your Answer


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.