Categories

10408

IE history to CSV

Back in April last year I wrote a post about viewing IE history

http://msmvps.com/blogs/richardsiddaway/archive/2010/04/13/ie-history.aspx

I had a comment left asking how this could be put into a CSV file

We’ll start by turning the script into an advanced function that outputs an object

function get-iehistory {            
[CmdletBinding()]            
param ()            
            
$shell = New-Object -ComObject Shell.Application            
$hist = $shell.NameSpace(34)            
$folder = $hist.Self            
            
$hist.Items() |             
foreach {            
 if ($_.IsFolder) {            
   $siteFolder = $_.GetFolder            
   $siteFolder.Items() |             
   foreach {            
     $site = $_            
             
     if ($site.IsFolder) {            
        $pageFolder  = $site.GetFolder            
        $pageFolder.Items() |             
        foreach {            
           $visit = New-Object -TypeName PSObject -Property @{            
               Site = $($site.Name)            
               URL = $($pageFolder.GetDetailsOf($_,0))            
               Date = $( $pageFolder.GetDetailsOf($_,2))            
           }            
           $visit            
        }            
     }            
   }            
 }            
}            
}


The main changes are to stop writing strings to the output and to create an object to put the data into.



Run the function by dot sourcing the file you have the script in – its easier to do this in ISE Then try these commands



get-iehistory
get-iehistory | export-csv iehistory.csv -TypeInformation
Import-Csv iehistory.csv

IE History

Saw a post on the forums that looked interesting.

Problem

Convert  this VBScript into PowerShell

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
HISTORY_LIST = 34
ITEM_NAME = 0
ITEM_DATE = 2
Set objShell = CreateObject("Shell.Application")
Set objHistory = objShell.NameSpace(HISTORY_LIST)
Set objHistoryFolder = objHistory.Self
Wscript.Echo vbCrLf & "Location of History"
Wscript.Echo objHistoryFolder.Path


For Each objPeriod In objHistory.Items

  Wscript.Echo vbCrLf & objPeriod.Name
  Wscript.Echo String(Len(objPeriod.Name), "=")

 If objPeriod.IsFolder Then
    Set objSiteFolder = objPeriod.GetFolder


    For Each objSite In objSiteFolder.Items
      Wscript.Echo vbCrLf & objSite.Name
      Wscript.Echo String(Len(objSite.Name), "-")

      If objSite.IsFolder Then
        Set objPageFolder = objSite.GetFolder

        For Each objPage In objPageFolder.Items
          strURL = objPageFolder.GetDetailsOf(objPage,ITEM_NAME)
          WScript.Echo vbCrLf & "URL: " & strURL
          strDateVisited = objPageFolder.GetDetailsOf(objPage,ITEM_DATE)
          WScript.Echo "Date Visited: " & strDateVisited
        Next

      End If

   Next

  End If
Next

 

The script came from http://gallery.technet.microsoft.com/ScriptCenter/en-us/28696fd5-2a90-4766-9b96-d4bc38c9db12 and interestingly was only stated to work on Windows 2000 and Windows XP

For my first go I did a straight conversion

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
$shell = New-Object -ComObject Shell.Application
$hist = $shell.NameSpace(34)
$folder = $hist.Self
$folder.Path

$periods = $hist.Items()
foreach ($period in $periods){
    ""
    ""
    $period.Name
   
    if ($period.IsFolder) {
        $siteFolder = $period.GetFolder
        $sites = $siteFolder.Items()
       
        foreach ($site in $sites) {
            ""
            $site.Name
           
            if ($site.IsFolder) {
                $pageFolder  = $site.GetFolder
                $pages = $pageFolder.Items()
               
                foreach ($page in $pages) {
                    $url = $pageFolder.GetDetailsOf($page,0)
                    $date =  $pageFolder.GetDetailsOf($page,2)
                    "$date visited $url" 
                }
            }
        }
    } 
}

 

We start off the same in creating a COM object and access special folder 34 – the IE history.

After displaying the folder path we get the periods and iterate through them, displaying the name – this will be the day of the week or Last week etc

We check if the period is a folder and if it is we get the items in the folder.

We can then iterate through them displaying the site name and checking if it is a folder.

if it is we can get the pages visited and display the URL and the date visited.

While this worked I was 100% happy with it because of the nested foreachs.  I wanted to do more on the pipeline so I had another go

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
$shell = New-Object -ComObject Shell.Application
$hist = $shell.NameSpace(34)
$folder = $hist.Self
$folder.Path

$hist.Items() | foreach {
    ""; ""; $_.Name
     if ($_.IsFolder) {
         $siteFolder = $_.GetFolder
         $siteFolder.Items() | foreach {
            $site = $_
            ""; $site.Name
            if ($site.IsFolder) {
                $pageFolder  = $site.GetFolder
                $pageFolder.Items() | foreach {
                    $url = $pageFolder.GetDetailsOf($_,0)
                    $date =  $pageFolder.GetDetailsOf($_,2)
                    "$date visited $url" 
                }
            }
         }
     }
}

 

This does exactly the same as the first attempt. It could be made more compact but is already getting a bit hard to understand. For once the more verbose way may be better.

BTW I did this on Windows 7

Start-Process

In this post - http://richardsiddaway.spaces.live.com/default.aspx?_c01_BlogPart=blogentry&_c=BlogPart&handle=cns!43CFA46A74CF3E96!2262 -  I showed different ways of starting a process.

Now that a post CTP3 version of PowerShell v2 is available in Windows 7 I can share this.  Thanks to however brought it to my attention.

If you are in PowerShell and need to kick off IE then just use

Start-Process www.microsoft.com

or better still one of these :-)

Start-Process "http://richardsiddaway.spaces.live.com"
Start-Process http://msmvps.com/blogs/RichardSiddaway/Default.aspx

Its all the little things like this that add up to PowerShell being so useful

Technorati Tags: ,,