{"id":55113,"date":"2008-09-05T02:20:00","date_gmt":"2008-09-05T02:20:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/09\/05\/hey-scripting-guy-how-can-i-tell-when-i-last-logged-on\/"},"modified":"2008-09-05T02:20:00","modified_gmt":"2008-09-05T02:20:00","slug":"hey-scripting-guy-how-can-i-tell-when-i-last-logged-on","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-tell-when-i-last-logged-on\/","title":{"rendered":"Hey, Scripting Guy! How Can I Tell When I Last Logged On?"},"content":{"rendered":"<p><SPAN class=\"Apple-style-span\">\n<H1><SPAN class=\"Apple-style-span\">\n<H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! I came into work this morning and my computer was running, and it was logged in. Of course I have a locking screen saver, and the screen was locked, but I am positive that I logged out and shut down my computer last night before I went home. Now I am wondering if someone logged into my computer. I know about the security logs and all that stuff, but&#8230;okay, here is the question: in other operating systems I have used, they tell me when I last logged on. If they can do that, surely Windows can do that as well, right?<BR><BR>&#8211; TW<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hi TW,<\/P>\n<P>So you cannot remember if you shut your computer down last night or not, and you are worried. Sometimes I cannot remember if I shut mine down last night or not. My first thought as I was reading your letter was, &#8220;Hey, we can query the security log and look for things such as failed logon attempts, or look in the system log for system restarts.&#8221; But you said you simply want to know when was the last time you logged on to the computer.<\/P>\n<P>The easiest way to do this is to create a script that writes the current time to the registry, and then place that script in the&nbsp;<B>Startup<\/B>&nbsp;group of your profile. Each time you log on to the computer, the script will query the time from the registry and display this value to you. It will then update the time value that is stored in the registry with the current time from your computer. Obviously this is not a security solution, but it does answer your question, and can perhaps help you with remembering&#8230;uh, what was I talking about?<\/P>\n<P>Oh, yes. There are two parts to our answer: the first part is the script itself and the second is the shortcut to the PowerShell script that we place in the&nbsp;<B>Startup<\/B>&nbsp;group of your profile. First the&nbsp;script: <\/P><PRE class=\"codeSample\">$command = &#8220;LastLogon&#8221;\n$scriptKey = &#8220;hkcu:\\software\\scripting\\$command&#8221;<\/p>\n<p>if(test-path -path $scriptKey)\n {\n  $newdte = (get-date).tostring()\n  $dte = (get-itemproperty -path $scriptKey -name date).date\n  set-ItemProperty -path $scriptKey  -name date -value $newdte  | out-null\n   &#8221;\n     Your  $command was $dte \n   &#8221;\n }\nelse\n {\n  &#8220;You have no record of $command.&#8221;\n  $dte = (get-date).tostring()\n  new-item -path $scriptKey  -force | out-null\n  new-itemproperty -path $scriptKey -name date -value $dte | out-null\n }\n<\/PRE>\n<P>We begin the script by creating a couple of variables. The first variable is the&nbsp;<B>$command<\/B>&nbsp;variable in which we store the string representing the command action we are documenting. Why did I do this? Well, a fundamental Scripting Guy principle is what I call the &#8220;Environmental approach to scripting.&#8221; Environmental? you ask. Sure. Reduce, recycle, and reuse. Reduce the amount of code you write by recycling scripts you have previously written, and reuse functions and subroutines you have squirreled away in other scripts. So as I write this script, I am thinking you may want to use this script to document when other commands may have been run as well. All you would need to do is change the value of the&nbsp;<B>$command<\/B>&nbsp;variable. The second variable we create points to the place in the registry where we will store this information. We choose the&nbsp;<B>HKEY_CURRENT_USER\\Software<\/B>&nbsp;hive, and we are creating the scripting key for our use. These two commands are seen&nbsp;here: <\/P><PRE class=\"codeSample\">$command = &#8220;LastLogon&#8221;\n$scriptKey = &#8220;hkcu:\\software\\scripting\\$command&#8221;\n<\/PRE>\n<P>When you log on to the computer and the script runs, it checks to see if the registry key exists by using the&nbsp;<B>Test-Path<\/B>&nbsp;cmdlet. If the key exists, we use the&nbsp;<B>Get-Date<\/B>&nbsp;cmdlet and convert it to a string by using the&nbsp;<B>tostring<\/B>&nbsp;method. We then store the string in a variable named&nbsp;<B>$newdte<\/B>. Now that we have the current time, we use the&nbsp;<B>Get-ItemProperty<\/B>&nbsp;cmdlet to read the date value from the registry and store it in a variable named&nbsp;<B>$dte<\/B>. We then write the date string held in&nbsp;<B>$newdte<\/B>&nbsp;to the registry using&nbsp;<B>Set-ItemProperty<\/B>&nbsp;and display the message telling you when your last logon was. This message is seen here:<\/P><IMG height=\"241\" alt=\"First run graphic\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey0904\/firstrun.jpg\" width=\"500\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>The code that performs the registry check, updates the registry, and displays the logon message is seen&nbsp;here: <\/P><PRE class=\"codeSample\">if(test-path -path $scriptKey)\n {\n  $newdte = (get-date).tostring()\n  $dte = (get-itemproperty -path $scriptKey -name date).date\n  set-ItemProperty -path $scriptKey  -name date -value $newdte  | out-null\n   &#8221;\n     Your  $command was $dte \n   &#8221;\n }\n<\/PRE>\n<P>There is another scenario: This is the first time you run the script. In this case, there will be no registry key, so there will be no value to display. We need to store the current date time, create the registry key, and display a notification message. The first thing we will do is store the current date and time. To do this, we will essentially use the same line of code we used earlier. We use the&nbsp;<B>Get-Date<\/B>&nbsp;cmdlet and turn it into a string. This is seen here:<\/P><PRE class=\"codeSample\">$dte = (get-date).tostring()<\/PRE>\n<P>Next we need to create the registry key and update it with the time value. When the registry key is created, it will look similar to the registry settings seen here:<\/P><IMG height=\"351\" alt=\"Registry graphic\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey0904\/TheRegistry.jpg\" width=\"500\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>To create the registry key and set its value, we need to first create the registry key, and then create the property associated with the key. To create the registry key we use the&nbsp;<B>New-Item<\/B>&nbsp;cmdlet and specify the path to the registry key. Next, we use the&nbsp;<B>New-ItemProperty<\/B>&nbsp;cmdlet to create the date property. These two lines of code are seen&nbsp;here: <\/P><PRE class=\"codeSample\">  new-item -path $scriptKey  -force | out-null\n  new-itemproperty -path $scriptKey -name date -value $dte | out-null\n<\/PRE>\n<P>Because we need to let the user know that no value was stored in the registry, we print out a string message seen here:<\/P><PRE class=\"codeSample\">&#8220;You have no record of $command.&#8221;<\/PRE>\n<P>When this command runs, the screen displays the friendly message seen here:<\/P><IMG height=\"248\" alt=\"Second run graphic\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey0904\/secondrun.jpg\" width=\"500\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>Putting the code together, this section of the code is seen&nbsp;here: <\/P><PRE class=\"codeSample\">else\n {\n  &#8220;You have no record of $command.&#8221;\n  $dte = (get-date).tostring()\n  new-item -path $scriptKey  -force | out-null\n  new-itemproperty -path $scriptKey -name date -value $dte | out-null\n }\n<\/PRE>\n<P>The trick to making all this work was the shortcut we created to run the script. The easiest way to create the shortcut is to go to the<B>Startup<\/B>&nbsp;folder in your profile, right-click the folder, and click&nbsp;<B>New Shortcut<\/B>. Specify the path to the script, and click&nbsp;<B>OK<\/B>&nbsp;until the shortcut is created. You now have a shortcut to the script. The problem is that the default file association for a PowerShell script is Notepad. Therefore, when you log in, the script will be displayed in Notepad. Not exactly what you had in mind.<\/P>\n<P>To change this behavior we need to specify that we want to execute the script in PowerShell. To do this, open the&nbsp;<B>Properties<\/B>&nbsp;of the shortcut, go to the&nbsp;<B>Shortcut<\/B>&nbsp;tab and edit the target by typing&nbsp;<B>PowerShell.exe -noexit -noprofile<\/B>&nbsp;in front of the path to your script. This will cause PowerShell to launch the script, leave the PowerShell prompt open, and not use your profile. If you wish to have your profile, you can delete the&nbsp;<B>-noprofile<\/B>&nbsp;command. If you can read really really fast, you could leave the&nbsp;<B>-noexit<\/B>&nbsp;switch off as well; just don\u2019t blink when the script runs. This figure below displays the trick for making the shortcut launch your PowerShell script:<\/P><IMG height=\"469\" alt=\"First run graphic\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey0904\/theshortcut.jpg\" width=\"500\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>Well, TW, I hope this script helps you to remember whether you logged off or not before you left for home. Now I know how I&#8217;m going to remember my wedding anniversary&#8230;<\/P>\n<P><FONT class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><SPAN class=\"Apple-style-span\"><B><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/B><\/SPAN><\/FONT><\/P><FONT class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><B><\/B><\/FONT><\/SPAN><FONT class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><\/FONT><\/H1><\/SPAN><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I came into work this morning and my computer was running, and it was logged in. Of course I have a locking screen saver, and the screen was locked, but I am positive that I logged out and shut down my computer last night before I went home. Now I am wondering [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,26,3,45],"class_list":["post-55113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-registry","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I came into work this morning and my computer was running, and it was logged in. Of course I have a locking screen saver, and the screen was locked, but I am positive that I logged out and shut down my computer last night before I went home. Now I am wondering [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=55113"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55113\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=55113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}