Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Windows Logoff GPO script/batch file to clear my documents and desktop, leaving shortcuts intact.

Posted on 2011-02-16
2
1,678 Views
Last Modified: 2012-05-11
Hi,

I am currently looking for a logoff script or batch file that i can place in our group policy for certain AD accounts. I need to clear the desktop and my documents of these accounts at each logoff, however i must ensure that it does NOT DELETE shortcut/link LNK files on the desktop.

Many thanks in advance

0
Comment
Question by:barryscott007
2 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34923653
Hi there, try this to delete all items from Desktop and My Documents, without deleting .lnk files from the Desktop.

Please test it on a test system first.

Regards,

Rob.
' THIS LINE MUST BE PLACED IN THE MAIN CODE TO ALLOW THE DICTIONARY
' OBJECT TO REMAIN PERSISTENT FOR THE ISMEMBEROFGROUP FUNCTION
Dim objMemberships

Set objNetwork = CreateObject("WScript.Network")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
Set objShell = CreateObject("WScript.Shell")
Set objShellApp = CreateObject("Shell.Application")

Const DESKTOP = &H10&
Const MY_DOCUMENTS = &H5&

strGroup = "Test Group"
If IsMemberOfGroup(objUser, strGroup) = True Then

	strFolder = objShellApp.Namespace(DESKTOP).Self.Path
	' DeleteFiles accepts a certain file extension of files to NOT delete
	DeleteFiles strFolder, ".lnk"

	strFolder = strFolder = objShellApp.Namespace(MY_DOCUMENTS).Self.Path
	' DeleteFiles accepts a certain file extension of files to NOT delete
	DeleteFiles strFolder, ""

Else
	'WScript.Echo "You are not a member of " & strGroup
End If

Sub DeleteFiles(strFolderPath, strExcludeExtension)
	For Each objSubFolder In objFSO.GetFolder(strFolderPath).SubFolders
		DeleteFiles objSubFolder.Path, strExcludeExtension
	Next
	For Each objFile In objFSO.GetFolder(strFolderPath).Files
		If strExcludeExtension <> "" Then
			If Right(LCase(objFile.Name), Len(strExcludeExtension)) <> LCase(strExcludeExtension) Then objFile.Delete True
		Else
			objFile.Delete
		End If
	Next
	objFSO.DeleteFolder strFolderPath
End Sub

Function IsMemberOfGroup(objADUser, strGroupCN)
	If IsEmpty(objMemberships) = True Then
		Set objMemberships = CreateObject("Scripting.Dictionary")
		objMemberships.Add LCase("ALL"), 0
		If IsNull(objADUser.MemberOf) = False Then
			If TypeName(objADUser.MemberOf) = "String" Then
				objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0
			Else
				For Each strGroupName In objADUser.MemberOf
					objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0
				Next
			End If
		End If
	End If
	If objMemberships.Exists(LCase(strGroupCN)) = True Then
		IsMemberOfGroup = True
	Else
		IsMemberOfGroup = False
	End If
End Function

Open in new window

0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question