Using PowerShell in SharePoint for Fun and Profit (Tutorial)

so today’s this webinar is going to be powershell for fun and profit in sharepoint 2010 the fun and profit part might be a bit of a stretch but but it’s definitely gonna be a fun so first the very first part is my my vanity slide just to kind of let you all know who I am I am a SharePoint MVP and I have been since 2006 the MVP program MVPs aren’t necessarily the smartest people in the room and having spent a lot of time with them in the last 7 years they’re very rarely the smartest people in the room but what MVPs are is helpful and like to do things like this and teach classes and tweet help and all that kind of stuff so if you have questions about SharePoint or Windows or exchange or PowerShell or whatever if you see somebody who’s an MVP for that they’re good a good place to start a good resource Rackspace we have six SharePoint MVPs on staff and so we’ve got a lot of that kind of mentality we like to help we like to to mentor nurture along with doing speaking engagements and things like this I also write we published our sixth SharePoint book here a few months ago there’s going to be a link later in the slide deck for that hopefully though you’ve all picked that up it’s all within this you know within arm’s reach right now you kind of cuddle with it a little bit to make you feel better when SharePoint 2013 starts getting fuzzy but that is out there I’ll have a link for that I do some consulting all of the PowerShell things that I’m going to do today are the result of real-world things that I’ve needed to do when I first got into PowerShell the very first thing that I did was I bought Bruce Pat’s book on PowerShell Bruce is incredibly smart help develop PowerShell but the book wasn’t practical it was just educational and so all the PowerShell you’re gonna learn here is practical stuff that you could just use so our company website you can go to SharePoint Rackspace comm and find out all the cool things that the SharePoint team at Rackspace is doing training and consulting and all that stuff if you have any questions hopefully easy questions you send them to Todd Clint at Rackspace calm don’t be embarrassed about sending dumb questions stupid questions because I found they’re the easiest answer so so they’re all good send them that way I’m also on Twitter you can hit me up on Twitter I’ll try to answer questions provide any you know motorcycle maintenance answers whatever I’m pretty friendly guy also in my quest to become the king of all sharepoint media I also do a weekly net cast on Monday night at 8:30 central and it’s kind of like a this weekend sharepoint sort of thing with apologies to Leo Laporte if things that I’ve done the previous week and SharePoint or things that have happened in the community service packs that have come out other people’s blog posts that I found kind of a curator sort of thing that is live every Monday night at 8:30 p.m. Central and we’ve got a chat room where you can heckle the chatroom and there tells me I’m an idiot good time had by all you can stream alive and then after the fact you can go to my blog and download it if you want to watch it several times and really really get the meat of all of it and finally all of the content that I’m gonna talk about today including this great slide deck with that very glamorous picture of me on it the transcript of all the things that I do all the notes and everything are going to be available at Todd Clint comm slash sizzle now much like yourself I was surprised to find out that Todd Clint comm slash sizzle was not already taken I would assure that I would probably had something there already sadly I did not so they’ll all be there also I’m going to there’s already a link there for the registration to this webinar and that’s how you’ll be able to watch the recording so excuse me Justin mentioned that there’s gonna be a recording available that’s the link so if you lost the original link you can get it there so first off we’re SharePoint folks so why why do we care about PowerShell we would care about SharePoint well we all know that SharePoint’s got a huge footprint it’s got its fingers and everything so to be a good SharePoint administrator you got to be good with a lot of technologies sequel and Windows and AD and iis and all kinds of stuff like that and PowerShell is one of them and if you’re going to be touching anything that that has to do with Microsoft technology in the next few years you got to be comfortable with PowerShell I was at Tech Ed in New Orleans and I was sitting to one of Markman nasty sessions and he’s a Windows guy he’s not even a SharePoint he’s just a Windows guy and he said he was doing a Windows class and he said if you have no intention of learning PowerShell raise your hand and some people raised their hand and said good I just wanted to know who wasn’t gonna be here next year you know who was gonna be working at McDonald’s or you know bagging groceries

or whatever it is that integral to your job PowerShell is gonna be everywhere and as the Microsoft products progresses you see the new versions of things more and more stuff is falling back on PowerShell you so you just gotta get comfortable with it unless you do things are already doing faster better it lets you do things that you previously would have had to open up visual studio to do and how terrifying is that and and you kind of lose a little bit of your soul when you open a visual studio it’s been it’s been proven empirical studies and all that stuff it lets you do things they’re just some some settings in that that you can only tweak in PowerShell so things you can’t do in the UI so it exposes a lot of that kind of stuff and finally it’s just fun it’s just a good time you just you’re a better person when you use PowerShell that’s also been proven so that’s kind of why I’ve been braced PowerShell it’s it I was I was very much against it when I found out the sharepoint 2010 was gonna rely on it and then then i started playing with it and it won me over it won my heart so this is kind of a loose agenda though I’m not gonna follow it very well basically if you’ve already gone out to Todd Clint comm slash sizzle and with a URL like that how couldn’t you have you could you’ve already seen my slide deck there’s like nine slides and we’re all so this is all gonna be demos but this is kind of what we’re going to be doing with that so that’s the agenda first just a couple of key concepts I won’t remind everybody of this is sort of the share point for the powershell for dummies slide this is the I don’t know where to start I forgotten everything I don’t deal with PowerShell every day this slide is kind of the key things to remember get command that that addresses the biggest challenge of any command line environment is just discovering things where stuff’s at I’ve got a five year old daughter if I set her down a front central admin and let her click long enough she could find out how to create a new site collection she could click enough of the pretty pictures put her down in front of PowerShell bow she’ll be able to spell her name you know the cast name that kind of stuff that’s about it so get command is how you discover things how you discover the commandment that you’re looking for and all of the sharepoint commandlets the noun part will start with sp so the get command star SP star will get you the sharepoint commandlets and this session is focused on sharepoint 2010 because the bulk of the sharepoint consuming world is using sharepoint 2010 but all of this will work with sharepoint 2013 as well and sharepoint 2010 sits on top of powershell version 2 but all of this will also work with powershell version 3 which is what SharePoint 2013 uses they both have this get command so once you do the get command you can start looking through the commands and try to figure out which nouns are the ones you want to work with which verbs are those things that you want to do so on so forth the next thing to remember is help and help gets you help on command and I can tell you that every single time I try to figure something out my workflow is I run get command till I figure out the command loop that I want to use I run help on that command let to get an idea what it does and I do an examples it kind of shows me how to work it now a PowerShell much like share only goes by one name but its full name is Windows PowerShell and that kind of gives us an idea that it’s owned by the windows team and the windows team has some hard and fast rules about PowerShell and they so any Microsoft product that uses PowerShell has to conform to these rules one of those rules is that every command –let has to have an example has to be every single one of them so before the PowerShell team or the sharepoint team could put out sharepoint with all the powershell commandlets all 500 and however many had to have examples but so that’s good because then for everything we want to do we can walk through some examples the bad news is the PowerShell team missed the beat and they didn’t require that the examples actually work or be accurate and for the most part they are but there’s a few out there that don’t don’t work so if you do go out and do a get help on something and use examples if the example doesn’t work well that’s there’s a few that are like that but for the most part they’re gonna kind of guide you down the road and and get you where you want to go the next the next Amanda that I like to use is get member and once we figure out what kind of object we’re working with and I’ll talk about objects in a minute then get member tells us the members of that object the things that we can do to it the things that we can look at and so that’s kind of one of the first things that I do and I’m messing with a new object type is a get member kind of see what’s what it’s got get alias is another one of those things that you just got to know I’m not a big fan of it

but PowerShell derives some of the best things from the scripting languages that existed when it was developed and so it’s got some great things from das it’s got some great things from UNIX and the bash shells and sea shells and all that kind of stuff and one of the things it has is short names for stuff aliases so what one of the rules that the PowerShell team has is that every command –let has to be in the form of verb – noun get – command get – member new SP site so on and so forth so as you’re looking at people’s PowerShell on the internet is you’re going to people’s blogs and stuff like that you might start seeing some things that aren’t you know don’t don’t match that formula and you might see some crazy symbols in there as well percent signs and stuff like that yet alias is your rosetta stone for those you can run get alias and see if you know see what the thing you’re looking at that doesn’t fit is really an alias for something that does fit so like the percent sign is an alias for for each object so that’s a good one that helps you figure out what scripts are doing get out I don’t know so let’s let’s just get into PowerShell and do do some demos play around a little bit we get out of there we do have the chat room open there and I am watching that monitoring it with an eagle eye and so if you’ve got any questions about anything that I’ve been talking about feel free to throw them in there if you have general questions Justin might take them but if they’re if they’re PowerShell II questions I can see them and I’ll make you star and I mention on the webinar here okay so like I said this is the focus of this webinar is SharePoint 2010 so we’ve got just a regular SharePoint 2008 r2 server here lots of patches and all that stuff on here but we’ve got SharePoint and the way that we open up PowerShell for SharePoint is with this SharePoint 2010 management shell and it’s normally hidden under here but I do make a couple of adjustments to this and yeah and I should have undone this one just so I could show you how to do it again so I go to PowerShell a lot and if I’m logging into a SharePoint Server it’s probably to open up our shell of something cuz I’m gonna do as much remotely as I can so one thing that I do is I right-click on here and I say pin to the Start menu I want that that PowerShell link just as close as I can get it so I can jump in there as fast as I can and then another thing that I do is after that’s created I go into its properties under Advanced because if you’re in this class you you can handle the advanced stuff here you’re up for it and I click this run as administrator the SharePoint module when it comes into PowerShell assumes that it can run as administrator it needs to do things to the to the server so if you don’t do this you’re gonna get all kinds of errors you’re not going to know why so so that I don’t have to remember to right click and run as administrator I just change the shortcut and that puts this up here and that gets me of this little guy right up here so we’ll let that let that load up here so I mentioned that if you go to Todd Clint calm slash sizzle you’ll be able to get notes from everything that I’ve done today and part of what’s going to populate those notes is one of my favorite things in PowerShell this is this was one of the turning points for me with PowerShell was learning about the partial transcript so if I type start transcript what’s gonna happen is PowerShell is going to create a text file here that has everything that I type and everything the PowerShell tells me back so for things like this it’s obviously it’s great because I’m gonna start this PowerShell or this transcript and then when this webinar is done I’m gonna upload it to the sizzle site and you guys can download it and see all the stuff that I did from a day to day thing it’s great because number one it’s a good cya if something happens and you’re doing stuff with PowerShell and it breaks you can always just jump into transcript and see what happened and as a consultant one of the services that I offer is scapegoat apparently I get blamed for a lot of things when I jump on people’s farms and so this is a nice nice way to deflect some of that the other thing is hopefully after you get out of this this webinar that the PowerShell fires will be burning bright and hot in your soul and so you’re gonna jump out to PowerShell and you’re gonna try all kinds of crazy stuff and then the next day or the next week you’re gonna forget everything that you did so another great thing that I love about this transcript is after I figured something out and and forgotten it I can go back and look at my transcripts and hopefully it won’t take me quite as long to recreate that that magic so another

thing that I like to do just because you know I like to show off and all that is I like to change the title so let’s do something like this and that changes the title of my box that was free that’s got nothing to do with SharePoint no extra charge that did also show up I like to set my quick edit so I can easily have you pay stuff okay so now let’s dig into some of the the SharePoint stuff so we mentioned the Get Command bit so we can do get command and we can do star espy star I’m gonna pipe this through more so that it gets paged because there are five hundred and some of these little guys so we can see we’ve got all these now I also got all this other crud we can see this showed up that’s got nothing to do a SharePoint what are all these applications this is this is a bad idea so what I can do is I did see a couple that made sense so this looks good this looks good so I have a couple of options I can do get command noun SP oh that’s looking good because I told you before that all of the sharepoint commandlets the noun starts with s P now in SharePoint 2013 that’s not as here it here too quite as closely there are a couple of other products like I think distributed cache has a couple of commandlets that don’t start with sp but for 2010 you’re in good shape you can do that and this will give you all of your this nothing but SharePoint goodness in here the other thing you can do is all of these commandlets are part of the module that a PowerShell module we had to load so the PowerShell knew how to talk to sharepoint we can also find them that way so we can say show me all of the commandlets from microsoft sharepoint powershell same thing so we got got some options so someone in the chat room is saying okay thump up screen of frozen i was it was scary there for a minute but i lost you guys so whenever you’re trying to figure out things because there’s a progression with with powershell as you as you start to master it and the first step that i think is a very important step is to get familiar with how to use PowerShell is start using it to do tasks that you already do create site collections create service applications that kind stuff things that take you ten seconds in Central Admin spend the four hours figuring out how to do it in PowerShell I promise that window will get smaller and smaller but that’s kind of the first day just things that you’ve already need to do and know how to do start doing those in PowerShell as you start progressing you’re gonna start trying to think of new things to do in PowerShell or somebody’s gonna throw a thing at ya that can’t be done and you’re gonna jump into PowerShell and try to figure it out this whole get command thing is part of that so if somebody asks you know hey you know how do I install a a security trimmer you know for uh for a search well then you jump in here you see that that’s that’s your first first clue as to how to do that so knowing how to do this this get command and let’s just see what this command load does so then the next thing is help and that tells that was a really bad example let’s try this one instead as you can tell this was a very very rehearsed thing so we conceded this enable SP timer job enables a timer job that’s good that could make that make sense we can see all the parameters and all that we can see that it takes a bind from the from the pipeline that’s good and then we can see down here it gives examples so let’s let’s do that all right that makes sense so we get a timer job and then we enable it so this is kind of how the process goes what I’m trying to figure out new things in PowerShell that’s kind of the process that I go through now one of the the actual real-world things that I use this for is the health analyzer service so when you install SharePoint brand-new everything looks great to go to central admin and it feels like something’s missing and that something is the big ugly red bar across the top telling you that SharePoint is broken the reason for that is that health analyzers all run by timer jobs and those timer jobs don’t all run on the same frequency so some run hourly some run daily summer and weekly so what you end up with is this situation where after you install SharePoint everything looks fine but then a couple days later something pops up and then a week later something pops up and you have to wait you know a month or so for all those to fire so I got tired of that so I decided that I wanted to figure out if there’s a way in PowerShell to just run them all at once so I know that there are some

commandlets with timer job in the name so let’s see all of them okay so these are all pretty self-explanatory disable enable get set start okay so we’ve seen how get works you give it a name and so I did some poking around and all of the health analyzer timer job names had health – in them so I said show me all of the timer jobs get all the time or jobs my pace didn’t work I can type it out get me all of the timer jobs where the name is like health so this just says PowerShell loop through them all give you back the ones that have health in the name okay so there’s a bunch of them so it turns out there’s timer jobs that for each period and then it kicks off the health analyzer rules for the different for the different scopes webs all that kind of stuff so now we’ve got this now what do we do well we look up here we see disable enable no get no set no start start looks good what a start do just start do what I think it does yes yes it does so it takes a timer job from the pipeline which is what that is so it will take all these and it will start them and so I can with this one command I can run every health analyzer rule and then get back get that red bar up there so as a consultant and I’m not gonna run it because it’s gonna take some time on this this machine we’ve got other things to look at we don’t have time to wait for timer jobs but that so that kicks them all off then it’s a consultant that lets me get into a conversation with a customer about what each of those are some of them you can’t fix like the whole you know farm account as a local admin but it has to be for User Profile service that whole hysterical gag I’m gonna get some all upfront so they don’t perk up over the next few so that was a good example of an applicable thing that I learned going through that so one of the other things that I’ve been asked about is getting like a list of that taxonomy what everything looks like you know how many site collections there are that kind of stuff and so we know if we do our whole get command deal we know that the noun for a site collection is an SP site and we know we can do get SP site and get a list of all of our site collections so that’s kind of handy but what do we do with it and and and we haven’t given powershell any information on what we need to know so what I found was most of the time people wanted to know what the URL was what the template of the route web was that kind of stuff so I put together this little number right here let that run for a second and so I get this list of site collections like I’ve got here I pipe it through select object and I’m bad there I shouldn’t use aliases but I do and I say PowerShell this object here has a bunch of properties I want you to give me the URL which is this the owner which is this questionably not that I’m not sure why and then I did some fancy footwork here kind of showing off a little bit and I say use the label template so that shows up here and then I say take the site collections root web and give me its web template name and ID so a couple of things here we’ve got this get espy site how did I know how to do all that well this is our friend get member and this is all of the members of the get SP site for the SP site object right here now you can get it this way if you are comfortable with your administrative manlihood and womanly hood you can also go out to ms the end but if any of your co-workers or friends see you’re doing that they’re probably going to think you’re a developer and and stop talking to you but this gives me all of the type all of the properties for this object so we’ve got methods method are things that you can do to the object and properties are things about the object and the best description that I’ve ever heard about objects is to think about them as a car so if you’ve got an object type of car there are things that you can do to the car start the car stop the car drive the car off a cliff whatever and there are properties about the car color number of doors make model that kind of stuff and there are many instances of the car object there are three instances of the car object in my garage right now and one of them that it

that object’s color is black one of them it’s blue so on so forth so anytime I’ve got a new object that I’m working with or even old objects I run through get member and I find out you know what’s there and so that’s how I discovered the URL and the owner and that kind of stuff they are all down here so there’s URL owner is right there we’ve got the secondary owner which ironically is called contact and so that that shows up there so Larry in the chat room is asking are there any forums where people discuss how to do stuff like this I’d like to generate an inventory of the list items and documents in a site collects with URLs creators creation date last modified last access size number of versions stuff like that Larry you are a man you sound very demanding but fortunately Larry before the end of this webinar you will be able to do that exact thing now that if there’s other things you want to do we’ll have to see but that particular itch I’ll be able to scratch as for where there are forums for this MSD ends got some good forums that you can look at PowerShell org used to have a SharePoint forum but I don’t think they do anymore there’s I noticing that the the members list there’s a couple of PowerShell folks in there so if PowerShell org does have a link for that go ahead and throw those up I’m looking at you John for one of those but also Twitter lair if you’re on Twitter you can do a whole you know do the hashtag SP help and ask this kind of question and but we will get to exactly that thing so the way that I was able to formulate that select statement of they just went through here looked at the stuff if I wanted to export that out if I wanted to maybe replicate this to another environment I could use the export CSV command late’ and I could do the get help on that to find out you know what that the user looks like but I know what it looks like so I’m going to give it a path I’m going to say sites that CSV churning in the background wheels turning steam coming out and then if I go to this directory that was a rotten directory to put that in I’ve got this CSV file and so I can take this this has got all that information and for these ones where the owner didn’t come through I could edit stuff so I could this is the text file so I could do that and do that save it all out and then I could go the other way with it and I could say I don’t want to get them because I’ve already got them I want to create them how would I do that so this is a wrong window if I do get command and I say give me all the commands that have CSV so we’ve got convert from blah blah blah import CSV well we just did an export CSV so there’s a good place to start with that so if I do import CSV has saved games that’s important on my server there’s a CSV file that I just had it’s got all my information in it so now I can pipe that to new SP site I can spell spell that right actually I need to loop through it and actually I’ve got I’ve got this command in another window and we just paste it over I’m not going to run it because I don’t want to create them it’ll take too long but that command right there importing the sights so make the filename the same and then piping that through for each object and then for each one of those objects create a new SP site with the URL column which is we’re getting from their alias column that we’re getting from there and it will create the site collection so now this is not creating the route webs we would have to do that but we could of course script all that so and I’m putting that little hash tag at the beginning that hash tag is the PowerShell way to say this is a comment so that I can throw it on the command line here it’ll show up in the transcript but it’s not actually gonna run that’s what I’m doing there so we can also do the same sort of thing with webs if we wanted to so we can do get SP web same sort of thing do that same thing and we do format table so we can see it that’s a nice bill format so we can see that’s got all the information that we want so we can do the same thing we can export that out to a CSV we could

say export CSV can’t type with an audience CSV path webs dot CSV same deal and then we could use the import CSV new SP web and we could build out a very quick and big taxonomy this is good for test environments if you want to make sure that they look the same it’s also a decent auditing thing if you just want to know what’s there we’ve got that okay so that’s just kind of some some things that have come up that I’ve done in the past let’s move on to my next set of examples so one of the more popular blog posts that I’ve had well let’s let’s do this so if you want to know all of the blog posts that I’ve had to deal with PowerShell you can go to Todd client comms life PowerShell one of the more popular ones that I’ve had lately is this one on how to use PowerShell to warm up our web apps in SharePoint 2013 we’ve got that age-old problem where everybody that has SharePoint in the morning the first time gets a bad experience because the app pools have to warm up and we we know our app pools are these little devils right here these w3 WP processes every time somebody you know every night pi is recycles the the web app at pool so every morning somebody’s got to hit every one of them warm them up most people call you they interrupt your freecell games or watch and you know cats fall off the tables on YouTube whatever you do I’m not gonna judge it makes for cranky users so since SharePoint 2003 there have been numerous ways to handle this scripts you could run and programs you could download and all that and so as I was perusing through PowerShell v3 a while back I saw a new command ‘let invoke web request which does exactly what it sounds like and so you can use this to hit all of your app pools and warm them all up scheduled it do whatever but since this is powershell v3 and SharePoint 2013 awoke work for today’s demo and I forgot to tell Justin to queue up the sad trombone trombone sound for this but there is a way to do this so since the beginning of time since people communicated with smoke and by banging rocks together there has been a command-line utility for UNIX environments called W get that just gets things over the web and so for sharepoint 2010 if you don’t mind downloading this binary and it’s a dependencies you can do the same thing so I’ve done that for today so let’s look at this this is going to slow things down a little bit we see this web app here is this app pool so we can go here things are snappy lightning fast the way the way sharepoint is the way we’re used to seeing it and so if I go out to PowerShell and I do an I is reset that’s gonna go away that webapps gonna recycle and go away and now the next person that hits sharepoint is potentially going to be you know someone in accounting at 8 o’clock or whatever and they’re gonna whine and complain because sharepoint slow now we can warm this up this is Central Admin you’ll notice it’s shut down too we can warm it up by hitting that but that’s that’s slow that’s the thing we’re trying to get away from and that’s that one right there so if you have double you get if you download it and I’ve got it you need double you get itself which is that little command-line guy there and then you need the dependencies which are these guys right here if you don’t if you don’t get those it won’t work so what you could do is okay so there’s that we know that PowerShell has or the PowerShell can give us all of our web apps so if we go SP web app those are all the things that we want to warm up that’s the that’s the thing and so we’ve already got a list of the places so we just need to find a way to send that you know to something else so in the SharePoint 2013 version I just send that to invoke web request and we’re good but we don’t have that in 2010 so we use get W get instead and it looks a little something like this so we’re getting our SP webs we’re piping them through for each and we’re saying for each of those spit out the name just cuz I like to show off a little bit run W get against the URL of the web app so that’s this column right here and then we have to login if they’re not anonymous we have to login to them so I have chosen the search content account because that’s one of the accounts that’s gonna have access to everything and so I chose that one obviously this gives passwords in plain

text so you know do your due diligence on all that and then it just outputs it to a null document so now what we’re gonna watch for over here is we’re gonna watch for a w3 WP process that’s running as SP web app that’s gonna tell us our web app got don’t like that though I’m in the wrong directory always make sure you’re in the right directory especially if you’re doing this in front of a group of people Oh much better okay so that’s gonna run for a second there’s our app pool so Mike Robbins in the chat room for those of you who don’t know Mike Robbins is a celebrity he won one of the power scripting games competitions here in the last year big big PowerShell guy he asked could you just use invoke web request and pipe the output to out null with a scheduled task yes in SharePoint 2013 you can Mike but SharePoint 2010 runs on PowerShell v2 PowerShell v2 doesn’t have invoke web requests so I have to do this the song and dance here but your your the rest of your thing is absolutely right you could schedule the tasking windows you could do the out null thing to not have any output but that’s absolutely right so now if I go over here to SharePoint this is still gonna be fast because now my my web apps big now a couple of notes on just hydrating web apps every type of template has to do some pre compiling so if you’ve got team sites and publishing sites are gonna want to hit one of each all right so that’s one of my favorite ones that just hole you know people ask that all the time how do I get these big powerful not quite so slow all right so one of the things when I started doing this these PowerShell sessions a few years ago I did more of the beginner stuff kind of stuff we did the beginning of this but audiences got smarter I’m not convinced that I did but audiences got smarter and they kind of demanded better examples and things like that so last year when I was in Toronto at the ESPY summit I yeah ternal Goodes but partway through the session I’m just like start throwing out things that you can’t do let’s figure out how to do it in PowerShell just kind of and so I’ve kind of adopted that that mentality so one of the things that has come up when I’ve asked that is versions versions in documents now this isn’t as bad in SharePoint 2013 because of shredded storage but in SharePoint 2010 we had this this problem where if somebody enabled versioning on a document library but didn’t set a maximum number of versions then people could upload hundreds of versions of the same document and have just balloon inside of the content database and so then your weary SharePoint administrator would go in and set a maximum number of versions but what doesn’t happen is the documents that have too many versions don’t get pruned until somebody does something with them so let’s look at this this document library here we’ll look at our settings we are allowing major versions we’ve not set a limit see this all the time rookie mistake happens all over so what happens though so let’s go back and look at our uh go back and look at our thing so we’ve got a bunch of documents in here that I’ve been working on that one’s got oh my goodness it’s got six versions holy cow this text document it’s just gobbling up the space it’s got three versions so you can go in here and you can delete a version but if you’ve got a lot of document libraries a lot of documents that’s just not doesn’t scale very well also if you do anything to this document after you set the major version it will adjust it but again you have to touch every document so let’s go in here and let’s say we’re only gonna allow two but I’m not gonna save this but if I do that it’s not gonna prune out those old ones so this is one of those things that we’ve got since I brought this up two people in the chat room like oh my goodness that’s me – holy cow so this is one of the things that somebody shot now so this was a fun experience for me to figure out in PowerShell so how do we break this down well we know that the object we want to work on is a list item that’s the thing with the versions and so it kind of got a walk down that path now the smallest object that we have manless for in PowerShell is the SP web so we’ve got a SP site SP web but we don’t have SP list or SP list item or any that kind of stuff SP web is the smallest but we need to go farther than that so we’re gonna walk around and we’re going to talk about how to do that so to get that web

object we need to do get SP web and here’s our URL right here so we’ll do that okay so that came back we’ve got we’ve got a web that’s good so let’s assign that to a variable because we’re gonna be using that a lot now I don’t trust my PowerShell very much so I always double check this I always spit out the variable at the end to make sure there’s not empty doesn’t cost any extra so we discovered earlier that we can do get member to see what’s inside of that object so let’s do get member I’m gonna spare you all the stuff we know it’s not a method look-look-look words words words oh look at this there’s a property called lists I wondered if the lists property of a web gives us the lists and libraries hmm well that bears some investigation let’s check that out folks so we’ve got this web object I type Li tab lists let’s do it oh I don’t know I don’t know how fast that’s scrolling for you guys when you do that command it throws a bunch of crud on your screen and that’s because we’ve kind of gone past the rope you know with the old swim in a lake or whatever they’ve got the rope we’ve kind of slam a little past that we’ve gone past the point where the the SharePoint team has made things easy and friendly for us now we’re in the weeds and so we need to to be a little smarter so we’ve got this list thing here we’re gonna pipe that through get member just an object nothing special actually it’s two objects but that’s beside the point so we’ve got the library espy document library and SP list is the other one up there so we need to we need to kind of to wrestle these things in so each of these objects has a title proper so let’s go let’s do that let’s say select all these lists for us but don’t give us all the crud just give us the titles oh yeah that feels better so this is all the lists in that web and you’ll see that our trouble list is right that one right there we’ve got two ways we can get to that list from here we can do it by number so starting with zero because this is ordinal not cardinal zero one two blah blah blah I think is 11 we can do it like that but counting rows of text on the screen is kind of cruddy work you can also do it by title so we can do this PowerShell smart enough to piece this together okay so that’s our document library so we’re getting closer so let’s let’s set a variable to this whatever you do and I’ve been doing the up arrow and down arrow stuff a bunch cuz I’m just I’m comfortable with all this when you get this you’re like sweet that’s the thing I want don’t just hit home and do list equal because all you’re gonna get is the title and I don’t want to really call anybody out and embarrass anybody but that may have happened to somebody on this call once or twice but let’s see if we’ve got the list here and if we want to make sure that we did get the list and not just the title we can do the get member and see what the object type is at the top document library document library okay so now we need to figure out where all this version stuff is is heightened and so I know I’ll save you some time I just ran through here in a mic version major version limit major with minor version limit these are all pretty uh pretty good suspects to look at to see if it’s it’s what we’re looking for so and there’s also where’s that enable versioning okay so I’ve got lists enable versioning so you use all the way at the end that’s set to true yeah because we can see in the web page over here we’ve got a set to true now we can see how what’s our major version limit zero how embarrassing okay so let’s go out of this so Larry in the chatroom um use him as my example sounds like he’s going to be fighting this fight himself so we go in here we’ve got no limit so what he can do is he can go in here and he can say this major version limit is two that’s all you get for this document library for this list we can set that we need to update our our object so now that’s written so now we go in here we say what’s the major version limit to awesome more importantly if we come in here and refresh this page oh yeah that’s the stuff right there okay so we’ve got just two versions and if we go

back to the library they still have six versions so what’s going so we can go through and prune them all or you can check everyone amount check everyone back in but that’s for that’s for losers we’ve got PowerShell we’ve got we’ve got we’ve got powered our hands so what we can do is we can say okay let’s walk through all these list items and for every list item that has more versions than the maximum number that we’re gonna have delete the versions so first let’s let’s set a variable let’s say that max equals list major version limit and let’s check it out it’s too good okay so now let’s walk through the items in the list and for each one of them let’s let’s spit them out if they’ve got too many versions so that’s this right here so these are our problem children okay so knowing them isn’t enough but now we’ve got them in one spot now we can do something else with them so we can run them through a process we’ve already got their names we can chase them down and I’m gonna paste in the next bit of code here okay so same sort of thing so I’m getting all the items in the list for every one of those if that item which is what that means the dollar sign the powershell says the next thing is a variable the dollar sign says powershell keep your eyes out the very next thing is a variable this underscore says this variable it’s like a fill-in-the-blank variable so you’re going to use it when you’re looping through things so it’s just whatever the current turn ID object is so since if the current object which is a document if it’s version count is greater than max which is two up here we just spit it out but now we’re gonna take a little bit x2 action we’re gonna say we’re gonna set an index variable to the number of versions and then while that number is greater than the max ink decremented by one and delete that that version so I ran pretty quick not much to it if we go up here now and say okay how many are greater than the max none could it be could it be did we do it go back to our problem child we did now there’s only two versions so this is a great way to do this to clean all this out you could run it on all your lists I’ve got a couple of minutes left I’m sure there’s massive applause in the background I can’t hear it this just not the way GoToMeeting works but I’m sure you’re all whooping and hollering and having a good time learning all that I got a new from the chat room excellent so when I have so in the it’s never enough for some people call them Duane all woman says amazing how could I do it for two thousand seven never enough for you Duane never enough so 2007 didn’t come with PowerShell natively because when 2007 came out there was before PowerShell was out you can with with a thing called reflection you can bolt PowerShell into SharePoint 2007 but then and you can do all this it’s a little uglier because there aren’t commandlets written by the sharepoint team and all that so yes you can do it Duane exactly the way that I did though you have to hard-code some other things but the process is the same I will warn you a Duane you are playing with fire if you use PowerShell with sharepoint 2007 you’re playing with with chainsaws that are on fire because there’s no stoppers in there so be very careful doing a test environment first but then you can do it alright so the last thing that I wanted to talk about is as you start working through your PowerShell you’re moving through the steps of PowerShell you’re going to start one to do things I like this a lot so you’re saying this is great but I’ve got a bunch of webs and a bunch of lists if there was a way to package this and repeat it and all that kind of stuff there is and that word that word is function so what you want to do is you want to write a function now this is supposed to be scary so it’s okay that you’re scared but what you could do is you could write a function that did this now I didn’t write this one doesn’t do that this is kind of a just kind of a skeleton I’m gonna put this up on the sizzling site so you can use this as kind of a skeleton but this is gonna create a function called get SP list cuz we don’t have one right now and I hope I hope my previous tests don’t still show up in here okay so we don’t have any command lists that have the noun SP list so what we could do is we could write all of this all that version stuff in a

function and then pass it a web and loop through and all that kind of stuff so let’s see how this works let me show you why you should be excited about this and Justin I know you want the mic back in a couple of minutes on I’m going as fast as my fingers will take me so I’m gonna save it in this directory and I’m gonna save this as not there here a little bit here so I’m going to call this TK function dot ps1 it actually already exists because I testing earlier okay so I’ve got this TK function since I told PowerShell I’m writing a function I need to introduce it somehow because I can’t you know I can’t do get SP list doesn’t show up okay so I need to dot source this and you do this like this so I’ve got a dot that says PowerShell load this and then dot slash means the one in this directory so now I’ve got this function that I just created so this just gives me a list gives me all the lists in a in a web so I can run now I did some fancy things here and I did this to kind of show you guys again to make this a skeleton for you we can take so I told them I want to take parameters I’ve told it that the parameter URL is mandatory because we can’t do a list we can’t get a list if we don’t know where we’re getting the list from and I’ve also done some tricky stuff here I’ve told PowerShell to validate it for me and then if everything works it’s just gonna spit out all my lists so let’s do this but first let’s do this and that might not work so let’s do get espy list I’m just gonna copy it out here and there’s all the lists in my web so PowerShell doesn’t come with that sharepoint didn’t come with it but we were able to write it so let’s see what some of the fun stuff that I did was let’s say that I messed up the URL blah blah blah I didn’t write any of this this is all PowerShell and that’s what this did this said validate this script take the input and run a get espy web against it and if it doesn’t come back true if it doesn’t show you that there’s an espy web there PowerShell just go ahead and yell at the user for me so it’s saying can’t validate argument URL blah blah it did all that so I so I know I have to have a URL but what if I don’t know the the parameters that I need with a normal powershell command like and just do a dash and tab through it how are shells gonna do that for me now so if I tab it tabs to URL so I know that I need that if I’m if somebody else is getting it and if I don’t do anything if I just type it it’s gonna say hey I need the URL it’s mandatory so then I can pipe it in and I can do that but obviously once I get that far I can type anything in this command block that I need and I can write whatever this is an interesting thing you see it’s not giving me the URL up here well that’s a bad example up here since I wrote this as a function PowerShell is giving me all of its framework so I can do verbose and then it gives me a verbose stuff it gives me all the regular verbose things but then I can put my own notes in there so I can put my own debugging stuff into the right verbose so this is one of the things as I started one of the the head guys in the PowerShell community you should be watching is a guy named Don Jones and one of his mantras is tool making make tools that you can reuse them and writing functions is one of those things so I will put this out on the sizzling site for you guys as well