Freigeben über


Packing up My Documents: A PowerShell Story

I am frequently asked by colleagues and customers for a dump of all of my SharePoint content (presentations, white papers, etc.)  While I continue to add files like these to my hard drive in various different locations, it has started to take me a while to aggregate them all forward.  It dawned on me that this need not be a half an hour exercise of locating documents.

Bing!  PowerShell

Thanks to another colleagues blog:  https://blogs.msdn.com/daiken/archive/2007/02/12/compress-files-with-windows-powershell-then-package-a-windows-vista-sidebar-gadget.aspx

I created a few functions and added them to my PowerShell module that loads with my profile.  Now it’s as simple as:

Get-ChildItem -path c: -filter *sharepoint* -recurse | create-zip "c:\sharepoint.zip"

#Special Thanks to: 
# https://blogs.msdn.com/daiken/archive/2007/02/12/compress-files-with-windows-powershell-then-package-a-windows-vista-sidebar-gadget.aspx

# Create a new zip file from pipeline
function Create-Zip()
{
    param
    (
        [string]$zipFile
    );
   
    New-Zip -zipfileName $zipFile
    $zip = Get-Zip -zipfileName $zipFile
   
    #loop through files in pipeline
    foreach($file in $input)
    {
        #add file to zip and sleep 1/2 second
        $zip.CopyHere($file.FullName)
        Start-sleep -milliseconds 500
    }

}

#create a new zip file
function New-Zip
{
    param([string]$zipfilename)
    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    (dir $zipfilename).IsReadOnly = $false
}

#get the zip file
function Get-Zip
{
    param([string]$zipfilename)
    if(test-path($zipfilename))
    {
        $shellApplication = new-object -com shell.application
        $zipFullFilename = (get-childitem $zipfilename).FullName
        $shellApplication.NameSpace($zipFullFilename)
    }
}