locked
Unable to get length of the folder/file correctly RRS feed

  • Question

  • Hi,

    I have a requirement to get the list of all shared folders on computer which are not accessed in last 6 months. also I have to get export the foldername, its acl and its size.

    I have written the script as below. the path and access is coming properly but unable to get the size correct.

    $computer=Get-Content C:\temp\computerlist.txt
    $arr='IPC$','D$','c$','ADMIN$','print$'
    foreach($pu in $computer)
    {
        "computername:$pu"
        $dpath= Get-WmiObject Win32_Share -Computer $pu |where-object{$_.Name -notin $arr}|select path -ExpandProperty path
        $dpath
        foreach($p in $dpath)
        {
            "Foldername:$p"
            $sh=get-childitem -Path $p -Recurse|Where-Object{$_.lastwritetime -lt ((Get-Date).AddMonths(-6))}|Select-Object pspath -ExpandProperty pspath
            "Filtered folder:$sh"
            foreach($s in $sh)
            {
                $s
                $acl=Get-Acl -Path $s
                $row = New-Object -Type PSObject -Property @{
                Size=$s.Length
                FolderName=$acl.path
                access=$acl.accesstostring
                }
                 $row|export-csv c:\temp\drivedata.csv -Append
             }
             
        }
                
    }

    Please guide me where am I missing it.

    Friday, June 16, 2017 6:23 AM

Answers

  • Hi,

    I modified something based on your script to make it more readable and genteel:

    $computer=Get-Content .\computerlist.txt
    $arr='IPC$','D$','c$','ADMIN$','print$'
    
    foreach($pu in $computer)
    {
        "computername:$pu"
        $dpath= Get-WmiObject Win32_Share -Computer $pu | ? {$_.Name -notin $arr}|select -ExpandProperty path
    
        foreach($p in $dpath)
        {
            "Foldername:$p"
    
            $sh=get-childitem -Path $p -Recurse | ? {$_.lastwritetime -lt ((Get-Date).AddMonths(-6))}|Select-Object pspath 
    
            "Filtered folder:$sh"
    
            foreach($s in $sh)
            {
                $acl=Get-Acl -Path $s
                New-Object -Type PSObject -Property @{
                Size=(Get-ChildItem -Path $s).Length
                FolderName=$acl.path
                access=$acl.accesstostring
                } | export-csv c:\temp\drivedata.csv -Append
            } 
        }                 
    }

    Hope you like it!

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Hello_2018 Thursday, June 22, 2017 7:03 AM
    • Marked as answer by TechnicalSeeker Thursday, June 22, 2017 10:21 AM
    Monday, June 19, 2017 7:32 AM

All replies

  • Folders do not have a length.  You will need to add up the lengths of all files in the folder.

    help measure-object -full


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Friday, June 16, 2017 7:55 AM
    Friday, June 16, 2017 6:40 AM
  • Hi,

    I modified something based on your script to make it more readable and genteel:

    $computer=Get-Content .\computerlist.txt
    $arr='IPC$','D$','c$','ADMIN$','print$'
    
    foreach($pu in $computer)
    {
        "computername:$pu"
        $dpath= Get-WmiObject Win32_Share -Computer $pu | ? {$_.Name -notin $arr}|select -ExpandProperty path
    
        foreach($p in $dpath)
        {
            "Foldername:$p"
    
            $sh=get-childitem -Path $p -Recurse | ? {$_.lastwritetime -lt ((Get-Date).AddMonths(-6))}|Select-Object pspath 
    
            "Filtered folder:$sh"
    
            foreach($s in $sh)
            {
                $acl=Get-Acl -Path $s
                New-Object -Type PSObject -Property @{
                Size=(Get-ChildItem -Path $s).Length
                FolderName=$acl.path
                access=$acl.accesstostring
                } | export-csv c:\temp\drivedata.csv -Append
            } 
        }                 
    }

    Hope you like it!

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Hello_2018 Thursday, June 22, 2017 7:03 AM
    • Marked as answer by TechnicalSeeker Thursday, June 22, 2017 10:21 AM
    Monday, June 19, 2017 7:32 AM
  • I would try it and see. Thanks.
    Thursday, June 22, 2017 7:08 AM
  • I would try it and see. Thanks.

    If you have any updates, welcome to post here.

    Have a nice day!

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, June 22, 2017 7:15 AM
  • It gave the desired output, thanks :)

    one more query, what can be done if some of the folder paths are more than 248 characters? how can I get data for this folders.

    Thursday, June 22, 2017 10:23 AM
  • It gave the desired output, thanks :)

    one more query, what can be done if some of the folder paths are more than 248 characters? how can I get data for this folders.

    the: get-childitem cmdlet contains two parameters -filter -include accepts wildcards can deal this.

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, June 23, 2017 4:51 AM
  • hi,

    Still I get the error

    get-childitem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260

    characters, and the directory name must be less than 248 characters.

    At line:13 char:13

    +         $sh=get-childitem -Path $p -Recurse -Filter * -Include * | ? {$_.lastwri ...

    Monday, June 26, 2017 6:51 AM
  • hi,

    Still I get the error

    get-childitem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260

    characters, and the directory name must be less than 248 characters.

    At line:13 char:13

    +         $sh=get-childitem -Path $p -Recurse -Filter * -Include * | ? {$_.lastwri ...

    You could try to modify the following location to unrestricted the limitation:

    Hit the Windows key, type gpedit.msc and press Enter.
    Navigate to Local Computer Policy > Computer Configuration > Administrative Templates > System > Filesystem > NTFS.
    Double click the Enable NTFS long paths option and enable it.
    If you're using a version of Windows that does not provide access to Group Policy, you can edit the registry instead.

    Hit the Windows key, type regedit and press Enter.
    Navigate to HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\ CurrentVersion\Group Policy Objects\ {48981759-12F2-42A6-A048-028B3973495F} Machine\System\CurrentControlSet\Policies
    Select the LongPathsEnabled key, or create it as a DWORD (32-bit) value if it does not exist.
    Set the value to 1 and close the Registry Editor.

    Note:Please backup your data before modifying registry in case data lose.

    Adding some useful links:

    https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Edited by Hello_2018 Tuesday, June 27, 2017 8:34 AM
    Tuesday, June 27, 2017 8:33 AM