Monthly Archives: November 2012

Logon Script is here to stay …

A login script is a set of instructions that are executed on the client computer when a user logs on. There are two ways to set up a Logon script. The first way is to type it in on the “Profile” tab of the user properties dialog in the Active Directory Users and Computers (ADUC), and the second way is to use Group Policy Objects (GPOs) to assign your logon script.

Why would someone choose one method over another?

You have to use ADUC if you have client computers with Windows OS older than Windows 2000. GPOs are not applied on the client computers with these operating systems. With clients that run at least Windows 2000, you have GPOs as an additional option. But if all of your clients have Windows 7, and your servers are running Windows Server 2008 R2, the above question does not constitute a dilemma. You know for sure that Group Policy Objects and the newer Group Policy Preferences (GPP) extensions offer the best possible solutions. Now we can change the question and ask:

Do we need a logon script?

Answer:       a) Yes

                      b) No

                      c) All the above

There is no an easy answer to this question. Microsoft does not provide an “official guidance” on whether we should keep using GPOs to perform most of the legacy configuration tasks that were previously performed exclusively by logon scripts. The answer should be founded on the organization´s needs, its size and complexity, as well as the experience and technical competence of the network administrators.

For now the good balanced answer to the question above would be that logon scripts should be used to handle those tasks that can’t be done with GPOs. Use the following as a general rule: to deploy or push a configuration change – apply GPOs or GPP; and use the exception to this rule: to collect information from client computers and servers – apply scripts.

What would be a good example of an exception to the general rule that would justify using logon/logoff script?

The good candidate would be a script that tracks user´s logon/logoff. To view the article with an example on the Microsoft site please follow this link:

Here I will show you my example of a logon and a logoff script created with PowerShell to help you create a monthly  rolling log file which contains information when and where each user Logs On and Logs Off.

The first one is the logon script:

# Filename: LogOnScript.ps1
# Date:           April 30, 2012
# Author:   Alex Dujakovic
# Description: Logon script to collect user Logs On
# **************************************************************
$ErrorActionPreference = "SilentlyContinue"
$strName = $env:username
$strComputerName = $env:ComputerName
$strTime = Get-Date -Format "t"
$strDate = Get-Date -format "yyyy-MM-dd"
$strFile = Get-Date -format "yyyy-MM"
$strFileName = "\\ServerName\LoginAndOutFiles$\"
# **************************************************************
$strFileName += [string]$strFile + ".txt"
$strDate + "," + $strTime + "," + $strName + "," + $strComputerName + "," + "Logon" | Out-File $strFileName -append -Encoding ASCII

And the second one is the logoff script:

# Filename: LogOffScript.ps1
# Date:           April 30, 2012
# Author:   Alex Dujakovic
# Description: Logoff script to collect user Logs Off
# **************************************************************
$ErrorActionPreference = "SilentlyContinue"
$strName = $env:username
$strComputerName = $env:ComputerName
$strTime = Get-Date -Format "t"
$strDate = Get-Date -format "yyyy-MM-dd"
$strFile = Get-Date -format "yyyy-MM"
$strFileName = "\\ServerName\LoginAndOutFiles$\"
# **************************************************************
$strFileName += [string]$strFile + ".txt"
$strDate + "," + $strTime + "," + $strName + "," + $strComputerName + "," + "Logoff" | Out-File $strFileName -append -Encoding ASCII

These scripts should be specified in a Group Policy. Logon and Logoff scripts run with the permissions of the user. The group “Domain Users” can be given permission to write to the LoginAndOutFiles$ share folder that has the log files. The rolling log file name is created in the following format: YYYY-MM.txt.  When I browse the content of the LoginAndOutFiles$ share folder, I can see the list of log files created for each month of the current year as shown in the picture below.


A monthly rolling log file with name 2012-11.txt will look like this:

2012-11-12,11:28 AM,Alex,Halifax-01,Login
2012-11-12,11:31 AM,Alex,Dartmouth-02,Login
2012-11-12,11:32 AM,Alex,Halifax-01,Logoff
2012-11-12,3:30 PM,David,Bedford-03,Logoff
2012-11-12,3:31 PM,Alex,Bedford-03,Login
2012-11-12,3:33 PM,John,Dartmouth-04,Login
2012-11-12,3:38 PM,Alex,Bedford-03,Logoff

Here I will provide another PowerShell script, named Get-LogFileInfo.ps1 which will help you analyze the log files. With this script you will be able to search the log files and retrieve information based on computer and/or user name.

Retrieves information from the log file that contains information of user(s) logon and logoff activity.
Get-LogFileInfo reads the log file specified and uses hash table to retrieve information from the
users logon and logoff activity on computers in a domain. The result is a table that dispalys
date/time, computer name, user name and action: logon/logoff.
.PARAMETER computer
The computer name, for which we want to dispaly logon and logoff activity.
.PARAMETER logfile
This is a mandatory parameter. You have to type the path and the name of the log file
that contains information of user's logon and logoff activity.
The user name, for whom we want to display logon and logoff activity.
Get-LogFileInfo -logname.txt
Assuming logname.txt contains logon/logoff information, this example dispalys the content of the log file.
Get-LogFileInfo -logname.txt -computer "Halifax-01"
This example retrieves information from the log file for a computer named Halifax-01.
Get-LogFileInfo -logname.txt -user "Smith"
This example retrieves information from the log file for a user named Smith.
Get-LogFileInfo -logname.txt -computer "Halifax-01" -user "Smith"
This example retrieves information from the log file for a computer named Halifax-01 and a user named Smith.


$obj = New-Object -TypeName PSObject
Get-Content -Path $logfile |
ForEach-Object {
                 $obj| Add-Member -Force -MemberType Noteproperty -Name "LogDate" -Value $_.Split(",")[0]
                 $obj| Add-Member -Force -MemberType Noteproperty -Name "LogTime" -Value $_.Split(",")[1]
                 $obj| Add-Member -Force -MemberType Noteproperty -Name "UserName" -Value $_.Split(",")[2]
                 $obj| Add-Member -Force -MemberType Noteproperty -Name "PCName" -Value $_.Split(",")[3]
                 $obj| Add-Member -Force -MemberType Noteproperty -Name "Action" -Value $_.Split(",")[4]

                 if(($computer -ne "") -and ($user -ne "")){
                        Write-Output $obj | Where-Object {(($_.PCName.Trim() -eq "$computer") -and ($_.UserName.Trim() -eq "$user"))}
                 elseif($user -ne ""){
                        Write-Output $obj | Where-Object {$_.UserName.Trim() -eq "$user"}
                 elseif($computer -ne ""){
                        Write-Output $obj | Where-Object {$_.PCName.Trim() -eq "$computer"}
                        Write-Output $obj
                } |
Format-Table LogDate, LogTime, PcName, UserName, Action -auto

The first example displays the content of the log file:
 The second example shows all log on and log off information for a user with name Alex:

The third example shows log on/log off activity on a computer with name Halifax-01:

And the last example dilsplays the result of our combianed search by computer/user name:

Please note that you do not need to use a PowerShell script to analyze the log files. Some times all that is needed is the following line typed into PowerShell console:

Select-String -Path ‘H:\ServerName\LogInAndOutFiles$\2012*.txt’ -Pattern “Alex”

The line above uses Select-String cmdlet to search all the log files contained in the folder named “LoginAndOutFiles$” and with the parameter “-Pattern” I’ve specified the text to find (“Alex”) in my monthly rolling log file. By default, this cmdlet finds the first match in each line and for each match, it displays the file name, line number and all the text in the line containing the match.

I hope that these scripts will encourage you to implement them in your organization and help you tackle tracking Logon / Logoff activity in your AD environment, which is somewhat a cumbersome process to do without the scripts.

Please note: Although the author has made every reasonable attempt to achieve complete accuracy of the content, he assumes no responsibility for errors or omissions. Also, you should use this information as you see fit, and at your own risk.