0

I am using PowerShell to compare the file count and size (per file extension) in 2 separate directories.

$User = $env:username
$pwd = pwd

clear
write-host "`n"
write-host "`n"
write-host "`n"
write "The current user is: $User"
write-host "`n"
write "The current path is: $pwd"
write-host "`n"
write-host "`n"
write-host "`n"
write "We need to know the following information:"
write "`n"
write "`n"

$UserDesktopPath = Read-Host "New PC User Desktop Path"          # This should be the new PC Desktop Path
$UserDocumentPath = Read-Host "New PC User Document Path"        # This should be the new PC Document Path
$USBDesktopPathServer = Read-Host "USB User Desktop Path"       # This should be the USB User Desktop Path
$USBDocumentPathServer = Read-Host "USB User Document Path"      # This should be the USB User Document Path

clear
write-host "`n"
write-host "`n"
write-host "`n"
write "This is the results for your Desktop Folder Paths:"
write-host "`n"
    $folder_new = Get-ChildItem -Recurse -path "$USBDesktopPathServer"                                                         # Recurses the New PC Desktop
    $folder_old = Get-ChildItem -Recurse -path "$UserDesktopPath"                                                              # Recurses the USB Backup Desktop
    Compare-Object -ReferenceObject "$folder_new" -DifferenceObject "$folder_old"                                              # Compares the two folders for the path to identify discrepancies


write-host "`n"
write "This is the results for your Documents Folder Paths:"
write-host "`n"
write-host "`n"
write-host "`n"
    $folder_new1 = Get-ChildItem -Recurse -path "$UserDocumentPath"                                                               # Recurses the New PC Documents
    $folder_old1 = Get-ChildItem -Recurse -path "$USBDocumentPathServer"                                                          # Recurses the USB Backup Documents
    Compare-Object -ReferenceObject "$folder_new1" -DifferenceObject "$folder_old1"                                               # Compares the two folders for the path to identify discrepancies


write-host "`n"
write-host "`n"
write-host "`n"
write "Now we shall compare file sizes of your Documents:"
write-host "`n"
write-host "`n"
write-host "`n"
write-host "`n"

function doc{
    $DirectoryDocuments = "$USBDocumentPathServer", "$UserDocumentPath"
    foreach ($Directory in $DirectoryDocuments) {
        Get-ChildItem -Path $Directory -Recurse | 
        Where-Object {-not $_.PSIsContainer} |
        Tee-Object -Variable Files |
        Group-Object -Property Extension |
        Select-Object -Property @{
            n = "Directory"
            e = {$Directory}
            },
        @{
          n = "Extension"
                e = { $_.Name -replace '^\.' }
                },
            @{
                n = "Size (MB)"
                e={ [math]::Round( ( ( $_.Group | Measure-Object Length -Sum ).Sum / 1MB ), 2 ) }
                },
            Count

    $Files |
        Measure-Object -Sum -Property Length |
        Select-Object -Property @{
                n = 'Extension'
                e = { 'Total' }
                },
            @{
                n = 'Size (MB)'
                e = { [math]::Round( ( $_.Sum / 1MB ), 2 ) }
                },
            Count
    }
}

When using the ISE and calling dtop I get the correct return:

PS C:\Users\Michael Nancarrow> dtop

Directory                       Extension Size (MB) Count
---------                       --------- --------- -----
D:\Deployment Kit\Test\Desktop2 txt               0     1
                                Total             0     1
D:\Deployment Kit\Test\Desktop1 txt               0    11
                                Total             0    11

Yet when run in the script, it does not return any value. I have attempted to call a function write $tst which runs dtop and that does the same (writes null).

Furthermore, I have removed the { so it does not run as a function, and it operates without an issue. My concern is perhaps the -Path file cannot be parsed at the same time as the input - meaning: when I call dtop from ISE it already has the $Directory variable stored in memory.

Are there any obvious errors here? I am rather new to PowerShell and am unsure where the mistake lies.

DankyNanky
  • 101
  • 4
  • Why are you creating two separate objects per folder/extension combination? – Mathias R. Jessen Aug 23 '17 at 12:22
  • Can't reproduce. When I put the code from your question in a PowerShell script, add definitions for `$USBDesktopPathServer` and `$UserDesktopPath`, and then execute the script, I get a list with the counts and cumulative file sizes for all extensions in the directories `$USBDesktopPathServer` and `$UserDesktopPath`. – Ansgar Wiechers Aug 23 '17 at 12:31
  • @MathiasR.Jessen Are you asking why 2 objects (such as size and extension) per `$USB...`? Sorry, I am a little thrown by the question. I am comparing two unique UNC paths for discrepancies (they should be alike) – DankyNanky Aug 23 '17 at 13:07
  • @AnsgarWiechers Yes - if I run `dtop` in ISE or alone, it works fine. However if I add it to a PS1 script and run `powershell .\~path` it does not output any data. – DankyNanky Aug 23 '17 at 13:08
  • Like I said, I can't reproduce the behavior you describe. Did you verify with a script that contains only the code from your question plus the 2 variable definitions? – Ansgar Wiechers Aug 23 '17 at 13:51
  • @AnsgarWiechers I have updated the code. – DankyNanky Aug 23 '17 at 14:25
  • I didn't ask for that. Did you verify with a script that contains only the code you originally posted plus the 2 variable definitions? – Ansgar Wiechers Aug 23 '17 at 15:17

0 Answers0