Delete recent files in Word 2013 with PowerShell!

This Monday one of my clients told me they have problems with a PowerShell script. This script is active on all desktops and runs every morning when the user logs on.
This script does something with three Word files in the user profile, so when a user logs on and opens Word it will see this three documents in the recent files, because there is no clean up after this. He asked me for a solution because a lot of users were complaining about this. I told him I could rewrite the script for them but the engineer responsible for this script was not on duty. So I thought let’s make a script this engineer can just paste under it or let this script somehow run after the other script. So my first guess would be to look on google, surprisingly I did not find somebody with the same problem.

Fortunately I was able to explore my way through PowerShell in the way that is told by Jeffrey Snover & Don Jones on Channel9. http://channel9.msdn.com/events/TechEd/NorthAmerica/2014/DCIM-B318#fbid=

With this I mean the basic knowledge you have to know to find your way in PowerShell, like Get-Help, Get-Command and Get-Member etc. Why you need to know this is all perfectly explained in the movie.

So I thought let’s clean up the registry where this information is stored by going there with a PSProvider.
Unfortunately I did get the information but the information was one object and it was not workable.
So then I remembered that if you work with Excel in PowerShell you can make a new-object and work with Excel properties. So let’s see if I can get the right information by doing the same with Word.
I got the information and could use this for cleaning up the values in the registry, yes I am not deleting the whole key, I just clean the value. If you delete the whole key and your file is in the first item, Item 1 is deleted and Word thinks there are no recent files anymore. Keep in mind that the script is written for Word 2013 so if you run a lower Office version change the registry key path.

At first I always declare the variables for easy reading and editing.
One rule I use is that a variable has to be used more than once in the script otherwise I will putt the value just in the script for better reading. This time I put them on top so It would be easy to give it to the costumer and he could just enter the file names there.

#declare variables
$File1 = ‘filename1.docx’
$File2 = ‘filename2.docx’
$File3 = ‘filename3.docx’

At first I create a new Word application object so I can use these properties.

$word = New-Object -ComObject word.application

Then I will do a count if there are any recent files, I do this by using the properties giving to me because I made the new object.

$RecentFilesCount = $word.recentfiles.count

If there are no recent files the value would be zero and the condition will only run if there is a higher value than zero.
if ($RecentFilesCount -gt 0)

Again I use the properties here to get the recent files in the list.

$RecentFiles = $word.recentfiles

Because I know which files I want to clean I do a check if this filenames exist in the just created variable $Recentfiles with a where-object and the declared variables in top of the script.

$MatchFiles = $RecentFiles|where-object {($_.name -eq $File1) -or ($_.name -eq $File2) -or ($_.name -eq”$File3″)}

When I capture this files I then want to know the property Index, so I do this by using select-object and the property Index.

[array]$Index = $MatchFiles|Select-Object -Property Index

Because this output gives me a table with name and I just want the values, I specify the property index to get rid of this tablename.

$collection = $index.Index

Finally I clear all the items in the collection by using a foreach loop. I use the foreach loop and not foreach-object because it’s a small task.

Foreach($item in $collection)
{
#Delete registry value by Item number
clear-itemproperty -path “hkcu:Software\Microsoft\Office\15.0\Word\File MRU” -name “Item $item”
}

}

Here I quit the Word application this is very import because otherwise the Word application will be keep on running as a background job on the system.

$word.Quit()

Here is the whole script.

#declare variables
$File1 = ‘filename1.docx’
$File2 = ‘filename2.docx’
$File3 = ‘filename3.docx’

#Make New Word application Object
$word = New-Object -ComObject word.application
$RecentFilesCount = $word.recentfiles.count
if ($RecentFilesCount -gt 0)
{
#Get recentfiles, Select the files by name and put there Index number in a Array
$RecentFiles = $word.recentfiles
$MatchFiles = $RecentFiles|where-object {($_.name -eq $File1) -or ($_.name -eq $File2) -or ($_.name -eq”$File3″)}

[array]$Index = $MatchFiles|Select-Object -Property Index
$collection = $index.Index

Foreach($item in $collection)
{
#Delete registry value by Item number
clear-itemproperty -path “hkcu:Software\Microsoft\Office\15.0\Word\File MRU” -name “Item $item”
}

}
$word.Quit()

Advertisements
Delete recent files in Word 2013 with PowerShell!

Rename unsealed Managements Packs in SCSM 2012 R2

Recently I started working on a SCSM project and in my enthusiasm I created Management Packs(MP’s) inside the Service Manager Console. This is not recommended practice because this sets the internal name to a GUID name in the management pack. The recommended way is to always use the Authoring tool to create a custom Management Pack and set the internal name yourself. Creating Management Packs in the Console is not a problem when only used in your own environment. But it does not help when searching or filtering the MP’s. But the real problem is when the need is there to export the MP’s to another SCSM installation, like from your test environment to your production.

So I needed to rename the unsealed Management packs I created.
Since I only found this link:https://technet.microsoft.com/en-us/library/hh519594.aspx

I thought it would be handy to write a small blog what I did to rename the MP’s.
I exported all the unsealed MP’s with the PowerShell command below and copy the one with a GUID name to a new folder so I can edit this MP’s and always have a backup. 

First import the Service Manager Module, make sure this is the directory where the module is located otherwise start up the Service Manager shell.

Import-Module ‘C:Program FilesMicrosoft System Center 2012 R2Service ManagerPowershellSystem.Center.Service.Manager.psd1’

Then export the MP’s, after parameter -path fill in the directory for storing the exported MP’s.

Get-SCSMManagementPack | where-object {$_.Sealed –eq $false} |Export-SCSMManagementPack –path

Then I renamed the values inside the XML file, by open the XML with notepad or some other editor.
I search on names with the MP GUID inside the file and replaced it there with the new name.
The new name cannot contain spaces so it would be something like this Contoso.Incident.Configuration.
Not forget to rename the file name also when its saved to Contoso.Incident.Configuration.xml

From:
For1
To:
to1

Then delete the old MP’s from Service manager and import the new ones, bada bing, bada boom!

As always test in a non-production environment for yourself first!

Rename unsealed Managements Packs in SCSM 2012 R2

Service Manager 2012 already registerted to data warehouse management group

I few weeks back I got a problem with Service Manager 2012.
The connector was gone at the Service Manager Console but on the data warehouse it was still registrated.
The error I got was, “A management group with the same name (xxxx) is already registerted to data warehouse management group “xxxx”

The way I fixed this, I unregistered the data warehouse connection with Powershell on the data warehouse server.
Because in Service Manager the connection is gone but at the data warehouse its still there.

The First command you should give to Powershell is the import-module “.\Microsoft.EnterpriseManagement.Warehouse.Cmdlets.psd1”

Then I took a look at the data warehouse which Managementgroup is still registered.
I did this with the command, Get-SCDWSource.

It shows something like this.

Then I type, UnRegister-SCDWSource –ComputerName serverDW –DataSourceTypeName “ServiceManager” –DataSourceName “SCSM”

Use for the variables the information that were shown when entered the Get command.
Then go back to the Service Manager console at the Service Manager server and register with Service Manager data warehouse again.

Service Manager 2012 already registerted to data warehouse management group