Find a computer’s model using PowerShell

Interestingly this blog’s most popular post is one where I demonstrate how to find the serial number of a PC in a batch file so you can write a script that does different things for different computer models.

I pretty much don’t write batch code any more, instead using PowerShell as much as possible. So I thought I’d add a post explaining how to get your computer model in PowerShell as well. If you’ve any experience using PowerShell then you know this could be a very short post…

And the answer?

(Get-WmiObject -Class:Win32_ComputerSystem).Model

Extending that to a specific model to allow us to work on something specific we have many options to play with. My preferred method if I’m only dealing with one model would be to filter at the point of making the query, like so:

Get-WmiObject -Class:Win32_ComputerSystem -Filter:"Model LIKE '%H77ITX%'" -ComputerName:localhost

Notice I’ve filtered the query using the LIKE operator which in WMI queries requires the % character as the wildcard indicator, not *. I’ve also specified the ComputerName as localhost in the above commands. Usually WMI commands operate on network objects, even if you only have the one PC, and as a result can take a little time to respond. If you specify localhost it speeds up the command and ensures you only get a result from the PC where you run your script. Of course if you want to do this on remote PCs then you’d use the ComputerName to specify one or more remote PCs.

Anyway that command is only going to return the object if we are running the query on the right PC model. That’s not too useful on its own so we need to make it return something like True or False, then we can work inside an If statement perhaps. Actually we don’t need to bother! If handles a returned object as if that means True, and no returned object as meaning False.

For example:

If(Get-WmiObject -Class:Win32_ComputerSystem -Filter:"Model LIKE '%H78ITX%'" -ComputerName:localhost)
{ Write-Host "Found an H78ITX model" }
Else
{ Write-Host "Model not found" }

OK so what do we do if we’ve got different models and we want to do different things on different models? Easy, go back to the first command, and use its output with the Switch statement.

$pcModel = (Get-WmiObject -Class:Win32_ComputerSystem).Model
Switch -wildcard ($pcModel)
{
    "*Latitude*" { # install special apps for a Dell }
    "*Elite*"    { # install special apps for an HP }
    default      { # install other things for everything else }
}

There’s plenty you can do with the switch statement, even using regex. There’s a good explanation of it here.

Of course you might want to do things for specific models, and also things for all models from a particular vendor. We can achieve that by being less specific with our initial command and just select different properties of the object from the variable like this.

$computerSystem = (Get-WmiObject -Class:Win32_ComputerSystem)
Write-Host $computerSystem.Manufacturer
Write-Host $computerSystem.Model

Hopefully that shows you how to get the model and manufacturer, then you can construct some logic around both.

Finally, something else to consider is that if you want to drill down using the serial number of a machine we need to make use of a different WMI object, namely Win32_BIOS, like this:

(Get-WmiObject -Class:Win32_BIOS).SerialNumber

PowerShell 3 for Windows 7, Server 2008 R2, Server 2008 and Vista

Following up on my post of a few years ago on PowerShell 2 being available, Microsoft have now released the bits needed to give you PowerShell 3 on Windows 7, Server 2008 R2, Vista and Server 2008. Sadly no PowerShell 3 lovelyness for XP, but then who really cares. I can barely remember how to use XP now I’m using Windows 8…

So without further a-do here is the download link for PowerShell 3:
http://www.microsoft.com/en-us/download/details.aspx?id=34595

Outlook 2007 – Faulty policy for deleting items on a delegated mailbox

When you have been delegated the right to delete items from someone else’s mailbox the default functionality is to have the deleted items go into your mailbox’s deleted items folder instead of the owners mailbox.
This functionality can be changed with a registry key, see here: http://office.microsoft.com/en-us/outlook/HA100750921033.aspx

HOWEVER, there is a group policy introduced with Office 2007 that can alter this functionality for all users. It’s located here:
“User ConfigurationPoliciesAdministrative TemplatesMicrosoft Office Outlook 2007Tools | Options…Delegates”
The policy is called:
“Store deleted items in the owner’s mailbox instead of delegate’s mailbox”

The help text says it needs to be enabled to set the functionality to have deleted items remain in the owner mailbox.
Guess what, it’s WRONG! Yes Microsoft have not tested it properly. It turns out if you enable the policy you enforce the default, to have deleted items go into the delegates mailbox, not the owner’s mailbox.
To get this policy to work properly you must set it to DISABLED.

When disabled, a policy key is created that enforces the alternate functionality for deleting mail.
HKLMSoftwarePoliciesMicrosoftOffice12.0OutlookOptionsGeneral
DelegateWastebasketStyle = 4

Hope this helps someone else!

PowerShell 2 for XP, Server 2003, Vista and Server 2008 is available!

Microsoft are calling it the “Windows Management Framework” but in reality this is PowerShell 2.0 and it’s now available for download: http://support.microsoft.com/kb/968929

If you install over the top of PowerShell 1.0 on XP the startmenu link will go but you’ll be left with the documentation links. So it might be better to uninstall 1 then install 2. Not sure yet.
Part of this is the new ISE (Integrated Scripting Environment) too and it’s wonderfully good. Just go start, run, then type: powershell_ise

Note: it supports only XP SP3, Server 2003 SP2, Vista SP1+, and Server 2008 SP2.
Windows 7 and Server 2008 R2 have PowerShell 2 built-in of course.

Update on 7th Sep 2012: If you want PowerShell 3 click here

You can’t add the IE icon to the Windows 7 desktop

Just recently discovered that it is not and will no longer be possible to add the IE icon to the Windows desktop, as of Windows 7! Detailed in KB article KB945402 Microsoft have decided to completely remove the old functionality that puts IE on the desktop as a proper desktop item – as opposed to a simple shortcut.
I guess it saves them from being sued again over IE being a part of Windows. Annoying though, damned EU courts, it’s all their fault. Oh well…

Download Orca for Vista

There’s a new version of Orca available (4.5.6001) that is suitable for Vista. You get it by downloading and installing the Windows Installer 4.5 SDK, available here (6.79 MB).

After the SDK is installed it will have delivered the installer for Orca into the program files folder (“%ProgramFiles%\Windows Installer 4.5\SDKTOOLS\orca.msi”). At this point you install it by double-clicking.
I recommend you archive the orca.msi file somewhere for the next time you need it.

Enjoy 🙂

Put Internet Explorer back on the Vista desktop

Microsoft keep being forced into how we are allowed to use Windows. With each new version of Windows and IE they’ve been removing IE from the desktop and making it harder to put it back.
I keep seeing the same thing all the time on users’ computers – they want IE on the desktop, can’t find out how to do it properly, and end up creating a shortcut to it. It does the job but they’ve lost the right-click functionality they had before
By properly of course I mean that like the Outlook icon (which they also took away – which I show how to fix here), you can right click on the Internet Explorer icon and get to your internet options easily.
Here’s how you get it back:-
Copy the following text into notepad, save it as a .reg file somewhere, right-click and import…

For all users on the computer:

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersionExplorerHideDesktopIconsNewStartPanel]
“{871C5380-42A0-1069-A2EA-08002B30309D}”=dword:00000000
[HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersionExplorerHideDesktopIconsClassicStartMenu]
{871C5380-42A0-1069-A2EA-08002B30309D}”=dword:00000000

For just the current user:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerHideDesktopIconsNewStartPanel]
“{871C5380-42A0-1069-A2EA-08002B30309D}”=dword:00000000
[HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerHideDesktopIconsClassicStartMenu]
“{871C5380-42A0-1069-A2EA-08002B30309D}”=dword:00000000

After you’ve done that, just refresh the desktop (click on it and press F5) and IE is back.

Find a computer’s model using the command line or in a batch file

Say you’ve got a script and you want to run something in that script that only runs if you’re on a specific type of machine, what can we do to find out what machine we are on?
Some might say “use vbscript and do a wmi call”. Well yes you could do that, but that’s needlessly hard! Use this simple command instead…

wmic csproduct get name

On my machine that returns two lines, one saying Name and another with my machine’s model name: HP Compaq dc7600 Small Form Factor
How cool is that!! Basically here we’re using a WMI command line tool. There’s lots to it, just type: wmic /?

So to use this in a script we’ll need to check for the existence of a particular machine name being returned, we can use the find command a different way and return the number of lines with a specific word in it. If we get 1 or more lines with that word then we’re on the machine we’re looking for.
Here’s an answer:

wmic csproduct get name | find /c /i "7600"

On my machine that returns a 1, so we just need to parse that into a variable and use a simple if statement and we’re there:

@echo off
for /f "delims==" %%a in ('wmic csproduct get name ^| find /c /i "7600"') do set /a machine=%%a
if %machine% geq 1 (
echo Running on a 7600 machine
) else (
echo Not running on a 7600 machine
)

[script now works, thanks to the comment pointing to the pipe-char issue]

You could modify that to do a goto to jump to another part of your script I guess. Just change the "7600" part for something that uniquely identifies the machine you’re looking for in your environment.

Ths works on XP and Vista. I think it should work in WinPE too as long as you have the WMI add-in installed.

— Updated 2011-10-06 —

And now here’s a better version of my script which will help people who want to use it to batch things a bit more easily!

@ECHO OFF
REM do a wmi query to get the info we want and put it in a variable
FOR /F "tokens=2 delims==" %%A IN ('WMIC csproduct GET Name /VALUE ^| FIND /I "Name="') DO SET machine=%%A
ECHO Computer model: "%machine%"

REM Now we have the model in a variable we can do some logic and run commands, for example...
REM Watch for stray spaces at the end, take out all spaces with: SET machine=%machine: =%
IF /I "%machine%" == "Latitude E6410" (
REM do something specific for an E6410
) ELSE (
REM do something for other types
)

— Updated 2013-09-21: new version of this article using PowerShell here

Add Outlook 2007 Icon to the Desktop

Back in the old days of Office 2000 and XP microsoft used to automatically put the Outlook icon on the desktop, and you could right-click it to get to profile and account settings. A very handy feature. Now with Office 2003, 2007 they’ve given up doing that following their ‘clean desktop’ trend. I understand that, but sometimes I need users to be able to get to Outlook easily and to manage their profiles without having to go to control panel – if I even allow them to go there!

I had to search hard to find this one but I found it in the end. Here’s the key needed to add Outlook 2007 back to the desktop:

Open regedit and browse to:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace
Then add a new key (not a new value) with the following name:
{00020D75-0000-0000-C000-000000000046}

Then refresh your desktop and your Outlook icon will appear (or disappear if you deleted the key).
Here’s the code if you want to put it in a .reg file:

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\{00020D75-0000-0000-C000-000000000046}]

I can confirm this works on Windows 7 Pro x86. It does not seem to work on Enterprise edition.

You cannot connect to the SMS database or expand nodes in the SMS Administrator console tree when you run SMS in Windows XP SP2

KB 841619 – when you can’t connect to SMS from an mmc console on a desktop

Here’s one that pisses me off royally, and another one that Microsoft seem unable to prove exists. At least there’s an answer though… Basically the scenario is: you give delegated access to SMS for a helpdesk user so they can provide remote assistance and diagnostics to users. You install the SMS console on their machine and when you connect and try to view the collections the egg-timer pops up, and you wait, and you wait, and then you get no collections! Zip, Nadda. Not a sausage. Eh!?

Well it seems to be caused by one things – XP Service Pack 2. The answer? Relax your security. Well done Microsoft…

Here’s the short of what to do to get around the problem:
1. Make sure the delegated user’s firewall is either off (if your in an Enterprise it probably will be off anyway) or make sure that tcp port 135 is open for your network, and %windir%system32wbemunsecapp.exe is added to the allowed applications list.
2. Run up dcomcnfg.exe, browse to “My Computer” in the console, choose properties of My Computer and choose the COM Security pane, and in the Access Permissions box click “Edit Limits…” and allow ANONYMOUS LOGON the Remote Access permission.
3. Now restart.

That should fix the issue. If it doesn’t then the user probably doesn’t have the right permissions in SMS, or the settings haven’t taken properly on the machine.
If you’re sure those settings taken then check that the user is a member of any groups that are in the “Distributed COM Users” local group on your SMS server/s.