Powershell: Set Logon Hours for all the users in an OU


With the script in this post you’re able to set logon hours to a bunch of users. All you have to do is to setup logon hours for a “template” user and define this “template” user into the $template variable.


The other step is to define the $ou variable with the path to the OU. In my case this was ict-freak/Gebruikers.

The script will now read the default logon hours and will apply them to the users in the OU.

$template = "" # This is a user with the default logon hours
$ou = "" # the full path to your ou "domainname/ouname1/ouname2"

# Get the logonhours from the template user
$template = Get-QADUser $template -IncludedProperties logonhours
[array]$logonHours = $template.DirectoryEntry.logonHours

# Get all users
$users = Get-QADUser -OrganizationalUnit $ou

# Loop through all the users
foreach($user in $users){
    Set-QADUser $user.Name -oa @{logonHours = $logonHours}


I found this trick here: http://www.powergui.org/thread.jspa?threadID=7860


How To: Uninstall Powershell 1.0


If you’re trying to install Powershell 2.0 CTP3 on a Windows PC with Powershell 1.0 already installed, the installation wizard will stop with the following warning.


Before the wizard will continue, you have to uninstall Powershell 1.0.

Open Control Panel. Select Show updates and browse to Windows XP – Software Updates. Click on Remove to uninstall Powershell 1.0.


If you don’t have an entry in the Add/Remove Programs control panel, You can find the Powershell 1.0 Uninstaller here:

  • C:\WINDOWS\$NtUninstallKB926139-v2$\spuninst\spuninst.exe
  • C:\WINDOWS\$NtUninstallKB926139$\spuninst\spuninst.exe

After uninstalling Powershell 1.0 you’re able to install Powershell 2.0 CTP3.

Posh: Run gpupdate on Multiple Computers


I created a Powershell script that will get all the computers from a certain OU and run GPUpdate /force on these machines. This script uses the Quest Active Directory cmdlets and PsExec.exe. You need to install the Quest cmdlets first. If PsExec doesn’t exist, the script will download it to the c:\tools directory.

When you start the script, you have to enter the FQDN and the OU name:


The script creates a text file with al the computer names in it. This text file will be used with the PsExec.exe command.


You can find the script on poshcode.org

Restart VMware Tools on all Windows VM’s


After reading the post on http://www.virtualvcp.com/content/view/82/1/ about the VMware Tools status “not running” and in particular the part about the preferred work around:

I find that restarting the VMware Tools Service in the guest OS always gets by the problem, but loggin into every single VM that reports the wrong status for it’s VMware Tools could be a bit of a drag. So I choose to do this remotely rather that logging on to each VM.

From any Windows workstation/server, open a command pompt and run:

sc \\{vm-name-or-ip-address} stop "VMTools"
sc \\{vm-name-or-ip-address} start "VMTools"

I thought that can be done via Powershell and the VI Toolkit. So I created the following script that will restart the VMware Tools service on every running Windows VM.

$vCenter = Read-Host "Enter the vCenter servername"

Connect-VIServer $vCenter

$Service = "VMtools"
$VMs = Get-VM | Where-Object {
        $_.PowerState -eq "PoweredON" `
        -and `
        $_.Guest.OSFullName -match "Windows"
foreach($VM in $VMs)
    Write-Host "-------------------------------------------"
    Write-Host "Restarting the VMware Tools Service on" $VM
        $Svc = Get-WmiObject -Computer $VM win32_service `
        -filter "name='$Service'"
            $Result = $Svc.StopService()
            sleep 5
            $Result = $Svc.StartService()
    Write-Host "Done.. "
    Write-Host "-------------------------------------------"

Disconnect-VIServer -Confirm:$false

This script generates the following output:


Other useful blog posts or kb articles on this subject:

Source for the restart service part in my script: http://blog.geekpoet.net/2008/10/manipulating-remote-services-with.html

Powershell: Get-SubFolderSize


On every home directory will be a  folder called Files. I was curious how big these folders are. So this is why I created the following Powershell script:

function Get-Foldersize{
  $Dirs = Get-ChildItem -path $StartFolder\* -Recurse -Include $SubFolder 
  foreach($Dir in $Dirs){
  $objFSO = New-Object -com  Scripting.FileSystemObject
  "$Dir = " + "{0:N2}" -f (($objFSO.GetFolder("$Dir").Size) / 1MB) + " MB" 

Get-FolderSize C:\Users Files


The output of the script will look like this:


Here are some pictures (as hard evidence 😉 ) of the folder properties:


Source: http://www.microsoft.com/technet/scriptcenter/

Beta: Vizioncore Virtualization EcoShell


Vizioncore released the beta version of the Virtualization EcoShell.

The goal of the Vizioncore Virtualization EcoShell is to provide a freeware desktop application for novice and expert IT administrators leveraging Windows PowerShell scripts across their multi-platform virtual environments.   

Fostered and supported by The Virtualization EcoShell Initiative (VESI) – an online community-driven Web site sponsored by Vizioncore – the Virtualization EcoShell is enhanced by the participation of community members through the exchange of new ideas, value-add services and extendable scripts. To become a member of the VESI community, please visit VESI Registration.


One of the best features is based on a script from Alan Renouf (vdiagram) and it’s called Generate vDiagram.


With this feature/script you can generate a Visio drawing of your VI environment. You can choose the following options:


If all the options are set to true, the script will generate three workspaces in Visio with three different vDiagrams. The diagram will look like this:


If you are managing a VI environment, this will be a must have tool. So take a look and get your download here: http://www.thevesi.org/downloads.jspa

Powershell: Add User to a lot of Groups


Update: Dimitry Sotnikov has posted a great article about the new cmdlets. You can read his post here.  After reading Dimitry’s post, I created a new script.

For some test purposes I created a new test user. This test user has to be a member off al my application groups in Active Directory. This job can easily  be done with Powershell and the Quest AD cmdlets.

$User = Read-Host ("Username")
Get-QADGroup -Name 'GG_APL_*' -NotContainsIndirectMember $User `
    | Add-QADGroupMember -Member $User


To verify the changes, you can run the following script:

$User = Read-Host ("Username")
Get-QADGroup $User | Select Name