I made some changes to sp aspx pages on a environment. I want to move these changes to my TFS. Usually one takes the pages using designer, save it to the desktop, and save that to TFS.

But is there a powershell way of automating this? - meaning just saving the desired aspx pages to the filesystem


I believe this is what you are looking for. You can export a specific file or object from the Export-SPWeb context, not the whole kitten kaboodle.

Export-SPWeb -identity "http://sharepoint" -ItemUrl "/default.aspx"  -Path "c:\default.aspx" 

Import-SPWeb -identity "http://sharepoint" -Path "C:\default.aspx" 
| improve this answer | |
  • Hi. But where do we specify the relative folder it might be in...for e.g. what if I am trying to download/upload http://localhost:5566/Section1/SubSec1/somepage.aspx? – deostroll Jan 21 '13 at 12:56
  • then your -ItemUrl will be section1/subsection1/somepage.aspx – Mike Jan 21 '13 at 14:15
  • when you import it, only the item specified in the export will be imported... – Mike Jan 21 '13 at 14:49

As I understood you need source of the file from content DB to check it into Source Control. Please, try the following code:

Add-PSSnapin Microsoft.Sharepoint.Powershell
$web = Get-SPWeb <path to web>
$file = $web.GetFile('<relative path to file>');
$bytes = $file.OpenBinary();
[System.IO.File]::WriteAllBytes('<path to file on your disk>', $bytes);
| improve this answer | |

you should use WebRequest

$web = Get-SPWeb $webUrl
$path = "C:\file.aspx"

$request = [System.Net.WebRequest]::Create($url)
$request.UseDefaultCredentials = $true

#do web request - if exception -> item does not exist
try {
    $response = [System.Net.WebResponse]$request.GetResponse();
    if ($response.StatusCode -ne "OK") {
        write-host "Error: " $response.StatusCode;

    # get contents
    $stream = [System.IO.Stream]$response.GetResponseStream();
    $streamReader = New-Object System.IO.StreamReader($stream);

    $data = $streamReader.ReadToEnd();

    $enc = [system.Text.Encoding]::UTF8
    $filebytes = $enc.GetBytes($data) 


    # save file        
    [System.IO.File]::WriteAllBytes($Path, $filebytes)
catch [Exception]
    write-host "exception" $error[0]


EDIT: I was in good mood so I've completed also the code to download contents and save them to file system.

| improve this answer | |
  • 2
    No. I don't mean this way. I am just trying to do the same operation as Export the aspx page from the SP designer. – deostroll Jan 17 '13 at 6:37
  • The chocolatey way is quite cool: @powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString($url))" – eirikb Jan 21 '13 at 22:25

Another approach is possible :

$web = get-spweb http://server/yoursite
$file = $web.GetFile("$($web.Url)/pages/home.aspx")
[System.IO.File]::WriteAllBytes("$(gl)\$($file.Name)", $file.OpenBinary())

This will write in the current directory the content of the file "Pages/home.aspx"

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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