background preloader

PowerShell

Facebook Twitter

Tip of the week. Here’s a quick tip on working with Windows PowerShell. These are published every week for as long as we can come up with new tips. If you have a tip you’d like us to share or a question about how to do something, let us know. The String’s the Thing One thing that separates Windows PowerShell from other shells (in particular the typical Unix shell) is this: while most operating system shells are text-based, Windows PowerShell is object-based. As you might expect, there are pros and cons to these two different approaches; as a general rule, however, it’s fair to say that Windows PowerShell requires much less text and string manipulation than its fellow operating system shells.

So does that mean that you never have to do text and string manipulation in Windows PowerShell? $a = "Scripting Guys" $b = "scripting guys" Comparing Two String Values So what kind of things do you need to do with string values? When we run this command and echo back the value of $d we get the following: What’s that? Owners Manual. This is your guide to getting started with Windows PowerShell. Read through these pages to get familiar with Windows PowerShell, and soon you’ll be driving around like a pro. On This Page Running Windows PowerShell Scripts Running Scripts From Within Windows PowerShell Even More About File Paths Bonus: “Dot Sourcing” a Script Running Scripts Without Starting Windows PowerShell See?

That Wasn’t So Bad Running Windows PowerShell Scripts Few things in life are as exciting as getting a brand-new command shell and scripting language; in fact, getting a brand-new command shell and scripting language is so exciting that you can barely get the thing out of the box before you want to take it for a spin. As it turned out, however, this is what happened: Hmmm, instead of running, your script opened up in Notepad.

As it turned out, however, this is what happens: File C:\scripts\test.ps1 cannot be loaded because the execution of scripts is disabled on this system. Wow; how nice. See? Well, almost right. How to send an email using a Windows Powershell script. If you're new here, you may want to start with my most popular posts. Then, subscribe to my RSS feed to stay updated. Thanks for visiting! Google Query: send email with powershell In a previous post, I showed you how you can schedule a Powershell script. This can be accomplished by using the Net.Mail.SmtpClient object. $emailFrom = "user@yourdomain.com" $emailTo = "user@yourdomain.com" $subject = "your subject" $body = "your body" $smtpServer = "your smtp server" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $smtp.Send($emailFrom, $emailTo, $subject, $body) That’s it.

Happy Coding! Technorati Tags: Windows, Powershell, Scripting, Programming ShareThis No related posts. Related posts brought to you by Yet Another Related Posts Plugin. Sharepoint Global Site Collection Administrator editor using PowerShell « dunxd.com. I finally got round to writing a PowerShell script which allows you to globally add or remove users from Site Collection Administrators. I had expected to be able to do this using stsadm commands, but it turns out you cannot use stsadm to remove a Site Collection Administrator (although you can add an administrator this way – and the Microsoft documentation suggestions suggests removal works too – it doesnt!).

Instead I had to delve into the Sharepoint object model and figure out where on earth the Site Administrators are set. Turns out it is in the root site (aka Web in the code) of the site collection, which has a SiteAdministrators property – a collection of SPUser objects. These need the IsSiteAdmin property setting to false. That’s how the script works. The script will also list out the Site Collection Administrators for all Site Collections in a Web Application, which is useful.

Just run the script, then quit it after you have the information you need. Hope you find that useful. List Computers using Powershell. Using Windows PowerShell, how can I retrieve a list of all the computer accounts in my domain? -- TE Hey, TE. You know, several years ago the Scripting Guy who writes this column was playing basketball with one of his brothers. Because there were a total of 5 people at the park they had to play 2-on-2, with one person forced to sit out.

After a short while a sixth person showed up and asked if he could play. He was a little guy, very quiet and unassuming, but for some reason the Scripting Guy who writes this column took one look at him and thought, “I bet he’s good.” “I’ll guard the new guy,” said the Scripting Brother. “I don’t know,” said the Scripting Guy who writes this column. “No,” said the Scripting Brother. So how did it go? About halfway through the game the Scripting Brother turned to the Scripting Guy who writes this column and said, “Do you want to switch guys?” The truth is, this script is nowhere near as bad as it looks; the problem is that it looks pretty darn intimidating. Powershell naming convention. Recently I posted about “smart” function names I am using – and we had great discussion about it :) I promised I will write some follow up afterwards and here it comes :) First of all, I would like to say that I BREAK naming convention of PowerShell and I am fully aware of it, however in my situation positives are outweighs negatives.

Because number of people that are not following naming convention is growing, I think there is chance there are some small gaps in PS design that leads to this situation. First let’s discuss what is naming convention for PowerShell functions: <Verb>-<Noun> Good and simple example is New-Object. Then official way to do so would be New-SCCMObject. According to official naming convention, retrieving all SCCM related functions can be achieved by calling Dir Function:*-SCCM*. You can find Microsoft document about PowerShell naming convention here and I highly recommend reading it. You shouldn’t change verb, only noun.

Get-User “Dmitry Sotnikov” | Set-User –city “St. How does return work in powershell functions. "Keith Hill [MVP]" news:4451264B-4BC4-42C1-87C4-175A450A41CF@xxxxxx > This is a common problem that folks with more traditional programming > experience when using functions in shell languages like PowerShell (or > Korn shell). My suggestion for this would be to leave the default behaviour of returning all non-captured values *unless* return statement is encountered. In that case return only what is explicitly specified. for example: function noret { 1 2 3 } returns an array of 3 values function withret { 1 2 3 return 4 } returns just the value 4 That would let the non-programmer users continue to get what they expect and allow us anal-retentive programmer types to get what we expect from it as well <g> If it's too far gone for that now then add a "returnonly" keyword that flushes the non-captured values.

How to: Deploy a Project Server Workflow. Published: May 2010 By using Microsoft Visual Studio 2010, you can easily create a SharePoint solution package (.wsp file) for a Project Server workflow. When you develop a workflow, Visual Studio 2010 creates the Features and Package folders in the workflow project (Figure 1). The Features folder includes the default Feature1 feature. Procedure 1. On a Project Server computer, run Visual Studio 2010 as an administrator, and then open a Project Server workflow solution. On the Package.package tab, if you imported any images, verify that the package includes the IMAGES1 (BranchingWorkflow) item and the image files.

The generated Feature.xml file that is in the BranchingWorkflow.wsp solution package includes the Id attribute and Scope attribute of the Feature element: The BranchingWorkflow.wsp file can be copied to other Project Server computers for installation. You can use Windows PowerShell commands to install and update a workflow solution package in a Project Web App site collection. Try...Catch...Finally Comes To Life - The Technical Adventures of Adam Weigert.

So, PowerShell has some good error handling, but being so used to .NET, I really missed my Try...Catch...Finally statements. Especially when I needed to make sure a block of code always executed. Well, after some playing, I think I have the solution! I've tested this function in a few different ways. I hope this turns out to be as helpful to someone else as it is for me.