locked
Last variable not being populated. RRS feed

  • Question

  • This is a weird one...

    I have a script to pull out the file, folder and byte count of two folder paths to export to csv for comparison. When the csv opens the last variable in the list is 'empty'. I can confirm this by running the variable manually however, if I run the command to populate the last variable in the list, it works! If I put another variable command after my last variable that one won't populate and the previous 'non-working' one does. To make it even more weird, if I change the file and save it, the script will populate all the variables ONCE! Next time it is empty again. Very annoying. The only reason seems to be because the last variable is next to the pipe command. Here is the full script:

    ### Declare variables

    $Sourcefolder ="c:\temp\temp"
    $DestinationFolder = "e:\temp"
    $SourceFiles = Get-ChildItem $Sourcefolder -Recurse -File | Measure-Object | ForEach{$_.Count}
    $SourceFolders = Get-ChildItem $Sourcefolder -Recurse -Directory | Measure-Object | ForEach{$_.Count}
    $SourceBytes = "{0:N0}" -f ((Get-ChildItem $Sourcefolder -Recurse | Measure-Object -Property Length -Sum).Sum / 1)

    $DestinationFiles = Get-ChildItem $Destinationfolder -Recurse -File | Measure-Object | ForEach{$_.Count}
    $DestinationFolders = Get-ChildItem $Destinationfolder -Recurse -Directory | Measure-Object | ForEach{$_.Count}
    $DestinationBytes = "{0:N0}" -f ((Get-ChildItem $Destinationfolder -Recurse | Measure-Object -Property Length -Sum).Sum / 1)|

    ##################This next line is only required to allow the variable above to be piped into the expression below #######
    #$TempBytes = "{0:N0}" -f ((Get-ChildItem $Destinationfolder -Recurse | Measure-Object -Property Length -Sum).Sum / 1)|
    #########################################################################################################################
    Select-Object @{expression={$Sourcefolder};Label="Source"},
                  @{expression={$SourceFiles};Label="sFiles"},
                  @{expression={$SourceFolders};Label="sFolders"},
                  @{expression={$SourceBytes};Label="sBytes"},
                                
                  @{expression={$Destinationfolder};Label="Destination"},
                  @{expression={$DestinationFiles};Label="dFiles"},
                  @{expression={$DestinationFolders};Label="dFolders"},
                  @{expression={$DestinationBytes};Label="dBytes"}|
                  
    Export-Csv -noTypeInformation  "c:\temp\list.csv"
    Invoke-Item "c:\temp\list.csv"



    • Edited by egand Thursday, December 13, 2018 1:13 PM
    Thursday, December 13, 2018 1:11 PM

Answers

  • Please format your code as code here in the forum. Use the code posting tool provided. Thanks.

    Should work easier like this:

    $Sourcefolder      = 'c:\temp\temp'
    $DestinationFolder = 'e:\temp'
    
    $Source      = Get-ChildItem -Path $Sourcefolder -Recurse
    $Destination = Get-ChildItem -Path $DestinationFolder -Recurse
    
    [PSCustomObject]@{
        Source      = $Sourcefolder
        sFiles      = $Source | Where-Object {-not ($_.PSIsContainer)} | Measure-Object | ForEach-Object {$_.Count}
        sFolders    = $Source | Where-Object { $_.PSIsContainer } | Measure-Object | ForEach-Object {$_.Count}
        sBytes      = "{0:N0}" -f (($Source | Measure-Object -Property Length -Sum).Sum / 1)
        Destination = $DestinationFolder
        dFiles      = $Destination  | Where-Object {-not ($_.PSIsContainer)}  | Measure-Object | ForEach-Object {$_.Count}
        dFolders    = $Destination | Where-Object { $_.PSIsContainer } | Measure-Object | ForEach-Object {$_.Count}
        dBytes      = "{0:N0}" -f (($Destination | Measure-Object -Property Length -Sum).Sum / 1)
    } |              
        Export-Csv -noTypeInformation  "c:\_temp\list.csv"
    Invoke-Item "c:\temp\list.csv"



    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Thursday, December 13, 2018 1:37 PM
    • Marked as answer by egand Thursday, December 13, 2018 2:20 PM
    Thursday, December 13, 2018 1:34 PM

All replies

  • Please format your code as code here in the forum. Use the code posting tool provided. Thanks.

    Should work easier like this:

    $Sourcefolder      = 'c:\temp\temp'
    $DestinationFolder = 'e:\temp'
    
    $Source      = Get-ChildItem -Path $Sourcefolder -Recurse
    $Destination = Get-ChildItem -Path $DestinationFolder -Recurse
    
    [PSCustomObject]@{
        Source      = $Sourcefolder
        sFiles      = $Source | Where-Object {-not ($_.PSIsContainer)} | Measure-Object | ForEach-Object {$_.Count}
        sFolders    = $Source | Where-Object { $_.PSIsContainer } | Measure-Object | ForEach-Object {$_.Count}
        sBytes      = "{0:N0}" -f (($Source | Measure-Object -Property Length -Sum).Sum / 1)
        Destination = $DestinationFolder
        dFiles      = $Destination  | Where-Object {-not ($_.PSIsContainer)}  | Measure-Object | ForEach-Object {$_.Count}
        dFolders    = $Destination | Where-Object { $_.PSIsContainer } | Measure-Object | ForEach-Object {$_.Count}
        dBytes      = "{0:N0}" -f (($Destination | Measure-Object -Property Length -Sum).Sum / 1)
    } |              
        Export-Csv -noTypeInformation  "c:\_temp\list.csv"
    Invoke-Item "c:\temp\list.csv"



    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Thursday, December 13, 2018 1:37 PM
    • Marked as answer by egand Thursday, December 13, 2018 2:20 PM
    Thursday, December 13, 2018 1:34 PM
  • Brilliant! Thank you very much.
    Thursday, December 13, 2018 2:21 PM