Category Archives: PowerShell
Getting Started: What are .ps1 files and how do you use them?
What is a .ps1 file?
If you want to save a series of PowerShell commands in a file so you can run them again later then you effectively creating a PowerShell script. This is simply a text file with a .ps1 extension. The file contains a series of PowerShell commands, with each command appearing on a separate line.
Why would you use a .ps1 file?
When working with SharePoint I find that most of the time to achieve something useful I’m writing several lines of PowerShell with the result of each line feeding into subsequent lines. The scripts often end up resembling source code more than traditional command line input. So using .ps1 files work well, you simply write up the commands and then execute the .ps1 file to run all the commands, make changes re-run until you end up with a script that does what you want and you’ve ironed all the bugs out.
Saving your commands to a .ps1 file also makes them portable in that you can take the file onto another computer (another SharePoint server) and execute the same .ps1 file on it. By making use of input variables, input files, and prompting for user input you can craft .ps1 files that perform common functions and you can get a lot of reuse without having to change the commands within the .ps1 file.
How do you run a .ps1 file?
You can’t just double-click on a .ps1 file in Windows Explorer to run it. The most common way to run the .ps1 file in from a PowerShell Command window. The syntax differs a bit from the old command line.
You can either specify the entire path to the file such as:
c:\temp\myscript.ps1
Or if the current directory in the PowerShell window is set to the directory containing the .ps1 file you can use the .\ shortcut to execute the script:
.\myscript.ps1
That’s it for a very basic intro to .ps1 files, happy scripting!
Getting Started: The Basics of Using PowerShell with SharePoint
I’m a SharePoint guy, and certainly no PowerShell expert. I know enough PowerShell to get things done in SharePoint from time to time. I can often go months without using PowerShell and talking to colleagues I don’t think I’m alone in the SharePoint community.
So if you are just starting out with using PowerShell to do something with SharePoint, or it’s been a while and you need a refresher on the basics then you’ve come to the right place.
Before we get started I’m writing this article based on SharePoint 2013 running on a Windows 2012 server.
The ground rules
1) Run your PowerShell scripts from the SharePoint server itself
It is possible to run PowerShell against a remote SharePoint server rather than on the server itself, but it requires some setup on the SharePoint server side and let’s face it, it’s not basic. If you need to do it you need to be searching for “Remote PowerShell in SharePoint”.
2) Use Windows PowerShell ISE (not SharePoint Management Shell)
PowerShell needs to be executed/run at a PowerShell Console or PowerShell Window – this window looks like a standard Command Line window and I don’t find it too inviting.
Instead of the SharePoint 2013 Management Shell, I use the Windows PowerShell ISE.
Why? It’s like comparing Visual Studio with Notepad. The ISE is an environment for developing PowerShell scripts that gives you nice syntax highlighting, debug with breakpoints, intellisense and more. It’s more like a development environment than a command line. I think you’d agree it looks slightly more advanced.
3) Ensure the SharePoint PowerShell snapin is loaded
When using the “SharePoint 2013 Management Shell” (the ugly black one) it automatically loads a “snapin” which is basically a PowerShell extension that gives you a series of commands for working with SharePoint objects. When you use the Windows PowerShell ISE it has no idea of SharePoint, so you need to load the SharePoint snapin manually. The simplest way to do this is just to add the following code snippet to the start of all your scripts.
if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null) { Add-PSSnapin Microsoft.SharePoint.PowerShell }
4) Save a script to file before you try to run it
Windows PowerShell ISE will have trouble running a script if you haven’t saved it to disk yet. PowerShell scripts are saved as files with a .ps1 extension.
5) Talk to SharePoint via the SharePoint PowerShell Cmdlets
Here’s a reference of all the SharePoint 2013 PowerShell Cmdlets you can use to work with SharePoint.
Your first script
Pre-flight checklist:
- Start Windows PowerShell ISE
- Add code snippet for loading the SharePoint snapin
- Save the script as a .ps1 file
You can now start cutting and pasting examples from the internet and modifying them to work with your environment. The following 2 lines get a handle on the SharePoint website at the URL http://vs-server12 and then output the ID of the website.
To run the script and see if it work, click the Run button.
Any output from running your script is shown in the output window below the script editor.
You’re on your way, just remember you can only access the SharePoint server on which you are running your scripts. Accessing remote SharePoint servers is possible but you need to do special magic stuff to make that happen
If you encounter an error along the lines of:
Get-PSSnapin : No Windows PowerShell snap-ins matching the pattern “Microsoft.SharePoint.PowerShell” were found.
You’ve ignored ground rule 4 and the pre-flight checklist by forgetting to save your script before running it (yes, I still do this too).
How to bulk upload/copy a folder structure and files to SharePoint
So you’ve got a bunch of files nicely arranged in a folder structure sitting on your computer or a network drive that you want to copy up to a SharePoint library and preserve the directory structure. On the surface this is a daunting task as you can’t just give SharePoint your folder structure and tell it to do the work for you, so what are your options.
Option 1: Manually via SharePoint IU
Recreate the folder structure in SharePoint and upload the files via SharePoint UI in batches doing a multiple file upload to one destination folder at a time. Not much fun for anyone, but it can be done.
Option 2: Commercial Tool
Try a 3rd party tool. Yes they will probably cost you but will get the job done and probably with a few extra bell’s and whistles like applying metadata to SharePoint columns during the process. Some of the key companies to take a look at would be:
- Metalogix http://www.metalogix.net
- AvePoint http://www.avepoint.com
- Sharegate http://en.share-gate.com
- Thinkscape http://www.thinkscape.com
Option 3: Open Source Tools
There are some open source projects going around that claim to handle these types of bulk uploads, here’s a couple that look interesting:
http://spbulkdocumentimport.codeplex.com/
http://spfileupload.codeplex.com/
Option 4: Script it Yourself
PowerShell is an awesome way to get things done quickly in SharePoint, I’ve written these scripts in the past that show you how to create folders and how to upload documents to SharePoint. With a bit of extension and effort you could roll your own scripted solution.
Option 5: Explorer View
The old ‘Open Library in Explorer View’ option in SharePoint may get you out of trouble if the directory structure and number of files is fairly small. Simply use the Explorer View to open the SharePoint library up in a Windows Explorer window and then copy/paste or drag/drop into the Window and the magic will start to happen
Option 6: OneDrive for Business
Now this is my preferred option for getting the job done, and the reason for writing this blog post! To achieve this one, browse to your library in SharePoint and click on the sync button to have the SharePoint library sync’d via OneDrive to your local machine.
After the sync has completed the SharePoint library (and it’s content including any folder structure) is now represented as a folder structure that you can easily get to through Windows Explorer.
Now it’s just a matter of copy/paste or drag/drop your local folder structure (that you want to copy to SharePoint) into this folder in Windows Explorer. Here is my folder structure I want to copy up to SharePoint.
So I can just copy the “My Folder Structure” folder and paste it into the SharePoint sync library location. This is super quick because it’s just copying between 2 locations on your local hard drive.
Now the real goodness happens. OneDrive will sync the entire folder structure including any content files up to SharePoint in the background. You will notice that the folders and files you have just pasted have a tiny icon in the left corner. This will display a green tick when the item has been created in SharePoint. This is awesome, your computer is still free to use, there’s no lockup. If you need to shutdown before the sync is finished, it will just pick up from where it left off. OneDrive’s mission in life is to keep your local folder in sync with the SharePoint library, and it will work tirelessly in the background to do this.
Another powerful advantage this method has over many of the other options I provided earlier is that it will work with Office 365 which you just can’t reach some of those other methods.
Now my disclaimer before I get people throwing virtual rotten fruit at me. I’m not suggesting you use this method to move an entire network drive to SharePoint. Yes, I advocate you should take the opportunity to clean up the data on your network drives and not just move the problem to SharePoint. I also believe in the use of metadata to classify content rather than using folders. Having said that, there are valid instances when you do need to perform an upload of many files and preserve the folder structure in SharePoint and I think the OneDrive approach is a pretty cool tool to add to your arsenal.
SharePoint PowerShell How To: Create SharePoint Test Documents in Library Folders for Load/Performance Testing
SharePoint PowerShell How To Series – Foreword
I often find myself scratching around the local drive of my development machines for remnants of PowerShell scripts to generate list & libraries in SharePoint with all sorts of different folder hierarchies to test the performance of code I’m working on to ensure it scales well once the folder & item counts start getting up to high numbers. So rather than keep scratching I’m going to start posting my scripts as I create and use them so I can find/reuse them later on and someone else might find them useful as well. I’m sure they won’t work under all scenarios and situations (and they are not designed to). They are just quick scripts that serve a specific purpose to me at the time and you may be able to tweak to fit your needs.
Create documents in root (or any folder) of a document library
Script description: Takes a file located on c:\ and uploads it to a folder in a document library (renaming it automatically on the way up) to generate 1000 documents in the SharePoint library.
# ---- Script settings ---- $sourceDocumentPath = "C:\temp\Test.docx" # Source document to spawn new documents from for the creation $newFilenamePrefix = "Test document " $newFilenameExtension = ".docx" $numberDocsToCreate = 1000 # Settings for the destination to create documents in $webUrl = "http://vs-server38" $docLibraryName = "Document Set Performance (6000)" $folderPathWithinDocLibrary = "/Doc Set Load Test 1001" # Leave empty e.g. "" to create documents in root folder of library otherwise specify path relative to root folder e.g. "/Testing/Folder A" # ------------------------- #Open web and library $web = Get-SPWeb $webUrl $docLibrary = $web.Lists[$docLibraryName] $docLibraryUrl = $docLibrary.RootFolder.ServerRelativeUrl $uploadfolder = $web.getfolder($docLibraryUrl + $folderPathWithinDocLibrary) #Open file $file = get-item $sourceDocumentPath $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead() # Create documents in SharePoint write-host "Creating $i documents based on the file $sourceDocumentPath" for($i=1; $i -le $numberDocsToCreate; $i++) { $newFilePath = $docLibraryUrl + $folderPathWithinDocLibrary + "/" + $newFilenamePrefix+$i+$newFilenameExtension write-host "Creating document: $newFilePath ..." $spFile = $uploadfolder.Files.Add($newFilePath, [System.IO.Stream]$fileStream, $true) } write-host "Completed" #Close file stream $fileStream.Close() #Dispose web $web.Dispose()
Related articles
- SharePoint PowerShell How To: Create SharePoint List Items (in Root and in Folders) for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Test Documents in Library Folders for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Document Sets for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Library Folders for Load/Performance Testing
- How to import (upload) an entire folder of files to SharePoint using PowerShell
SharePoint PowerShell How To: Create SharePoint Document Sets for Load/Performance Testing
SharePoint PowerShell How To Series – Foreword
I often find myself scratching around the local drive of my development machines for remnants of PowerShell scripts to generate list & libraries in SharePoint with all sorts of different folder hierarchies to test the performance of code I’m working on to ensure it scales well once the folder & item counts start getting up to high numbers. So rather than keep scratching I’m going to start posting my scripts as I create and use them so I can find/reuse them later on and someone else might find them useful as well. I’m sure they won’t work under all scenarios and situations (and they are not designed to). They are just quick scripts that serve a specific purpose to me at the time and you may be able to tweak to fit your needs.
Create document sets in the root of a document library
Script description: Create 6000 document sets within the “Document Set Performance Test” library of the “LocationContentTesting” site on the SharePoint Server “vs-server82”.
Note: this assumes you have made have the standard “Document Set” content type available in the library.
#Script settings $webUrl = "http://vs-server82/locationcontenttesting" $listName = "Document Set Performance Test" $numberDocSetsToCreate = 6000 $docSetNamePrefix = "Doc Set Load Test " ### Get web and list $web = Get-SPWeb $webUrl $list = $web.Lists[$listName] ### Get Document Set Content Type from list $cType = $list.ContentTypes["Document Set"] ### Create new Document Set [Hashtable]$docsetProperties = @{} # Create desired number of subfolders for($i=1; $i -le $numberDocSetsToCreate; $i++) { $newDocumentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($list.RootFolder,"$docSetNamePrefix$i",$cType.Id, $docsetProperties) write-host "$docSetNamePrefix$i ... created" } $web.Dispose()
Related articles
- SharePoint PowerShell How To: Create SharePoint List Items (in Root and in Folders) for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Test Documents in Library Folders for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Document Sets for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Library Folders for Load/Performance Testing
- How to import (upload) an entire folder of files to SharePoint using PowerShell
How to find the Version, Culture and PublicKeyToken of an Assembly (DLL) using PowerShell
In response to a question on my article How to install a DLL to the GAC on Windows Server 2012 using only PowerShell (without having to install SDK or Visual Studio) here’s how you can find the PublicKeyToken for a DLL using only PowerShell.
([system.reflection.assembly]::loadfile("c:\temp\MyDLL.dll")).FullName
The output of this PowerShell statement will provide the Version, Culture and PublicKeyToken as shown below.
MyDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=630d986a50055aa6
SharePoint Client Browser 1.0 released, bye bye preview!
Very handy tool that developers and admins can add to their toolbag. Similar to the SharePoint Manager 2010/2013 tool that has been a great resource for many years now. SharePoint Client Browser has the added benefit of supporting different credentials modes, remote SharePoint Sites and handy PowerShell integration.
Bram de Jager - Coder, Speaker, Author
Finally after 2 months I decided to build the 1.0 version of SharePoint Client Browser and released it to the community! Although the preview (beta) status did not prevent people from downloading it. The counter is currently set at 555 downloads since start of the project on the 2nd of July (only 2 months ago).
CodePlex project and download at https://spcb.codeplex.com/.
So what got changed? I guess almost everything changed from authentication support for default (username and password), SharePoint Online, anonymous and forms based all the way to almost complete coverage of the Client Side Object Model (CSOM). That’s a bit over the top, but the basics for Foundation are in the tool. New capabilities for future releases will focus on Server components like taxonomy.
Remote PowerShell for SharePoint Online and on-premise
A hidden gem is the PowerShell support. It’s very easy to start a PowerShell session and use…
View original post 167 more words