none
Find all files overs over $ in size and owner of file RRS feed

  • Question

  • I have create a powershell script to search a folder path to find all files over $size and get the owner of the file that is greater than $size then export to csv file. 

    here is the script that works on my desktop computer, but errors out on my 2k8R2 servers.

    $size = $( 
    Add-Type -AssemblyName Microsoft.VisualBasic
    [Microsoft.VisualBasic.Interaction]::Inputbox('Enter the smallest file size you want to include in MB','File Size ','300')
    )


    $searchpath = $( 
    Add-Type -AssemblyName Microsoft.VisualBasic
    [Microsoft.VisualBasic.Interaction]::Inputbox('Enter the Root Path to start search in','Search Path','c:\')
    )


    @arr = @()
    gci $searchpath -Recurse | Where-Object -FilterScript {$_.Length / 1mb -ge $size} | ? {$_.PSIsContainer -eq $false} | % {
        $obj = New-Object PSobject 
        $obj | Add-Member NoteProperty Directory $_.DirectoryName
        $obj | Add-Member NoteProperty Name $_.Name
        $obj | Add-Member NoteProperty Length ($_.Length / 1mb)
        $obj | Add-Member NoteProperty Owner ((Get-Acl $_.FullName) .Owner) 
        $arr += $obj
        }

        $arr | Export-Csv -NoTypeInformation "report.csv"

    Can you give me any help on this? Here is the list of errors i'm getting on my servers

    At C:\PS_Scripts\Space_Used_by_User.ps1:21 char:65
    +     $obj | Add-Member NoteProperty Owner ((Get-Acl $_.FullName) .Owner)
    +                                                                 ~~~~~~
    Unexpected token '.Owner' in expression or statement.
    At C:\PS_Scripts\Space_Used_by_User.ps1:21 char:64
    +     $obj | Add-Member NoteProperty Owner ((Get-Acl $_.FullName) .Owner)
    +                                                                ~
    Missing closing ')' in expression.
    At C:\PS_Scripts\Space_Used_by_User.ps1:16 char:121
    + ... eq $false} | % {
    +                    ~
    Missing closing '}' in statement block.
    At C:\PS_Scripts\Space_Used_by_User.ps1:21 char:71
    +     $obj | Add-Member NoteProperty Owner ((Get-Acl $_.FullName) .Owner)
    +                                                                       ~
    Unexpected token ')' in expression or statement.
    At C:\PS_Scripts\Space_Used_by_User.ps1:23 char:5
    +     }
    +     ~
    Unexpected token '}' in expression or statement.
    At C:\PS_Scripts\Space_Used_by_User.ps1:15 char:1
    + @arr = @()
    + ~~~~
    The splatting operator '@' cannot be used to reference variables in an expression. '@arr' can be used only as an argument to a command. To 
    reference variables in an expression use '$arr'.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : UnexpectedToken

    Thank you 


    • Edited by rdobbsokc Wednesday, January 17, 2018 2:42 PM
    • Moved by Bill_Stewart Monday, March 12, 2018 8:16 PM This is not "fix/debug/rewrite my script for me" forum
    Wednesday, January 17, 2018 2:42 PM

All replies

  • Please find the modified script. Let me know if u encounter any issues .

    $size = $( 
    Add-Type -AssemblyName Microsoft.VisualBasic
    [Microsoft.VisualBasic.Interaction]::Inputbox('Enter the smallest file size you want to include in MB','File Size ','300')
    )


    $searchpath = $( 
    Add-Type -AssemblyName Microsoft.VisualBasic
    [Microsoft.VisualBasic.Interaction]::Inputbox('Enter the Root Path to start search in','Search Path','c:\')
    )


    $arr = @()
    gci $searchpath -Recurse | Where-Object -FilterScript {$_.Length / 1mb -ge $size} | ? {$_.PSIsContainer -eq $false} | % {
        $obj = New-Object PSobject 
        $obj | Add-Member NoteProperty Directory $_.DirectoryName
        $obj | Add-Member NoteProperty Name $_.Name
        $obj | Add-Member NoteProperty Length ($_.Length / 1mb)
        $obj | Add-Member NoteProperty Owner ((Get-Acl $_.FullName).Owner) 
        $arr += $obj
        }

        $arr | Export-Csv -NoTypeInformation "report.csv"

    Wednesday, January 17, 2018 2:51 PM
  • This is what you are trying to do:

    Get-ChildItem $searchpath -Recurse -Directory |
    	Where-Object{$_.Length/1mb -ge $size} |
    	ForEach-Object{
    		[pscustomobject]@{
    			Directory  = $_.DirectoryName
    			Name	   = $_.Name
    			Length	   = $_.Length / 1mb
    			Owner	   = $_.GetAccessControl().Owner
    		}
    	} |
    	Export-Csv report.csv -NoTypeInformation
    Please use code posting tool provided.


    \_(ツ)_/

    Wednesday, January 17, 2018 3:05 PM