#PS Script that filters AD Computers and moves it to specified OU

I wrote this script to filter my Active Directory Domain for Computers beginning with “SMB-” and move them to an Organizational Unit (OU) with the name “SMB-OU”.

You can use the script but you have to change the OU, Computer filter and Domain so that it matches your search and domain criteria.

I use the Outfile command to create a logfile from the movement for documentation.

Get-ADComputer -Filter * -Properties name | #get all active directory computers
where { $_.Name -match ‘SMB-*’} | #with computername that includes “SMB-“
Out-File c:\move_resultates_before.txt #writes status before any computer will moved in output file
Get-ADComputer -Filter * -Properties name | #get all active directory computers
where { $_.Name -match ‘SMB-*’} | #with computername that includes “SMB-“
Move-ADObject -TargetPath “OU=SMB-OU, DC=Datacenter-Flo, DC=de” #and moves the computer to OU named SMB-OU in domain Datacenter-Flo.de (you have to change to your domain)
Get-ADComputer -Filter * -Properties name | #get all active directory computers
where { $_.Name -match ‘SMB-*’} | #with computername that includes “SMB-“
Out-File c:\move_resultates_aftwe.txt #writes status after the computers were moved in output file
 

At the moment there is no security and other filters included. Only quick and dirty but it’s working.

Microsoft Masterminds Episode 1: Jeff Wouters on the Power of PowerShell

Welcome to the first episode of tech talks with outstanding Microsoft community members. Most upcoming interviews will be with Microsoft Most Valuable Professionals (MVPs) but I am not limiting myself to those outstanding Microsoft tech experts. Take Jeff Wouters for example: Jeff is recognized by the Microsoft community as a PowerShell Magician – you want to learn why? Read on and/or hear … and enjoy!

 

Twitter: @JeffWouters
Blog: http://jeffwouters.nl/

Self summary Jeff Wouters:

Back in 2007 I’ve started working in IT. Between then and now I’ve worked at a variety of companies such as a system integrator, retail company (fashion), consultancy firm and software vendor.
Then, in 2011 I’ve made the big step and started my own company, Methos IT, where I specialize in Microsoft System Center, Microsoft Hyper-V, Microsoft Windows clustering, Citrix XenApp, Citrix Provisioning Server, Citrix XenDesktop and Citrix XenServer.
Since I’m not the first person with this focus area, I wanted to make myself as unique as possible in this field, so I added my two passions to this: High Availability and PowerShell.

And just to be clear: I’m crazy about PowerShell… the ease of which you can accomplish tasks with it and the power (especially the power…) you can gain on your environment by utilizing the shell is something I love and find truly amazing.

Since I like to share my knowledge and experiences, I’ve started blogging… mainly about PowerShell, but would never avoid a post about any other subject that piques my interest.

I hope you enjoy my blogs and if you see me at an IT event, don’t be afraid and start a conversation with me… usually I’ve had breakfast/lunch so I won’t bite :-D

Quote from: http://jeffwouters.nl/index.php/about/

Editorial processing done by Rafael Knuth

 

Me (left) with Jeff Wouters (right) in Hamburg, Germany during the E2EVC Conference 2012

 

You can hear the complete interview here:

Readable version:

Flo: Jeff, I am happy to talk with you! Can you tell us about yourself and about your work?

Jeff: I am living in Holland, my main focus is Hyper-V in SystemCenter and I ran into some limitations in the product, so I went to PowerShell. With a little bit of googling … sorry: binging … I was able to find a lot of documentation. The PowerShell community is huge, the information they share is even better, so I was able to create some easy scripts. Then I had two sessions with Don Jones, he is a PowerShell MVP … one session was about Active Directory management and one about PowerShell Remoting. I then saw the power of PowerShell and I was like: “I have to learn PowerShell!” Windows Server 2012 and SystemCenter 2012 just came out, and I see PowerShell integrated into EVERYTHING. Basically, administration tasks are possible in the GUI … you can still click it. But for all the fun stuff you have to turn to PowerShell and … I love it!

Flo: Give us some examples of the power of PowerShell.

Jeff: When I started with PowerShell, I used it the wrong way.  I knew VB Script and then I started to use PowerShell code with the VB Script way of thinking.  For example, with VB Script it took me about 20 lines of code, with PowerShell it was still 18 lines. Then another Microsoft MVP Jeffrey Hicks showed me how to use the pipeline, switch statements and stuff like that … and suddenly 18 lines of code became three. Even those three lines can be trimmed down to one line if you like, but then the code will become harder to read and you have to know PowerShell really well to understand it. Flo: What are the major improvements from PowerShell 2008 to 2012 … in my opinion it’s that the syntax didn’t change. Jeff: Ok syntax … well (laughter). A little bit history. PowerShell version 1 had basically one error: “access is denied”. So PowerShell version 2 came with a lot more errors like the errors explaining what you did probably wrong and stuff like that, which was a very good improvement. Next, PowerShell came out with Remoting and all produts were suddenly using PowerShell Remoting. When PowerShell version 3 came out, you had PowerShell workflow, based on the Windows Workflow Foundation (WF). It will basically enable you to execute tasks in parallel, because natively PowerShell does things in sequence, and is very powerful with bulk tasks like 3,000 live migrations. You want do that in parallel preferably.  Further you have got member enumeration, which is an easy way to use properties to an object to create.

Flo: Have you ever used PowerShell web access?

Jeff: Only in demos, not in the production environment yet because my current customer doesn’t have a fully functional PKI infrastructure.  When I do this, I want to do it secure. Opening PowerShell to the big bad world is not a good idea. I know the power of PowerShell and with one command I can take your entire environment down if your Remoting is enabled. So that’s not something I want to give to everybody.

Flo: Do you use PowerShell script environment or Notepad?

Jeff: When I write a very big script, functions and stuff like that, I use either Primo Script or PowerShell Plus. For scripting together with other system admins who are not very fluent with PowerShell yet, I use PowerShell ISE in version 3 because it has intelligence and it makes things lot more easily. When I need to do some quick and dirty scripting, I use Notepad.

Flo: Is there something PowerShell can’t provide?

Jeff: It’s there something it can’t provide? (laughter). If anything can be done inside a product, it can be done in PowerShell! A lot of things are even removed from the GUI and the only solution is PowerShell.

Flo: Can you give an example, please?

Jeff: Yeah, take Hyper-V. You are able to use affinity to set virtual machines together on a node or … even antiaffinity to separate them from each other. Let’s say for example a SQL cluster with two virtual machines … you do not want those same machines on the same node. That’s where you use anti-affinity and you can only configure that through PowerShell. You can configure affinity through GUI but it’s a lot of clicking all over the place and anti-affinity is basically two, three lines of code.

Flo: Dell has many PowerShell Plugins and CLIs such as iDRAC and Power Center. Are you using such tools to integrate your environment with PowerShell?

Jeff: Let’s say … if I was a big system integrator and I was using Dell products for all my customers, I would script it once and make it a standard set up, deploy it at my customer site, hit my script … sit back, take some coffee … and when I finished my coffee my entire environment is up and running. For most system admins I would recommend using SystemCenter. Lots of system admins currently do not understand PowerShell, they don’t want to and they will never do so in their opinion.

Flo: You should change that …

Jeff: That is exactly my challenge. In Holland, I am doing some workshops in the evening to make customers enthusiastic about PowerShell. I recently had a workshop with a company that does hosting, and they have more than 300 web servers … Windows Server IIS and they deployed them manually. They have an automated deployment through PXE boot but enabling those server roles, enabling IIS packages and importing them, that’s all done by hand! CLICKING! So I asked them to give me some of the packages, I prepared a demo and the entire writing of the script took me 40 minutes. Deploying it was: hitting enter and sitting back.  Right now I can use that single script, put the packages in a certain directory and … I wouldn’t say it’s universal … but any package you put in that directory, you save it there, you hit the script, you sit back and your package for the website would be deployed on all you web servers. So, it’s not 300 times clicking. It’s pressing enter, sitting back and you are done. All you have to do is preparation for the script and then the next thing is enjoying the coffee.

Flo: We should definitely catch up shortly … and show customers what PowerShell can do on Dell products.

Jeff: Yeah, sure!

Flo: Sounds good! Thank you very much and … talk to you soon.

Blogs and Persons which Jeff mentioned during the Interview:

Don Jones

Twitter: @concentrateddon
IT Pro Column: http://www.windowsitpro.com/topics/powershell-scripting/don-jones-on-powershell
Don’s Shell Hub: http://shellhub.com/

Jeffery Hicks

Twitter: @JeffHicks
Blog: http://jdhitsolutions.com/blog/
Jeffs Guide “Learn PowerShell 3 in a Month of Lunches now available”: http://jdhitsolutions.com/blog/2012/11/learn-powershell-3-in-a-month-of-lunches-now-available/

PowerShell Community’s

http://powershell.org/
http://powershell.com/cs/

PowerShell Script Libary on Microsoft TechNet

http://msdn.microsoft.com/en-us/library/dd878324(VS.85).aspx

Microsoft Script Guy Blog

http://blogs.technet.com/b/heyscriptingguy