PS help with adding if statement to update File extension value.

Albert Edelstein 91 Reputation points
2021-11-01T14:26:27.573+00:00

Hi Everyone,
The scrips exports Folder, subfolder files and their attributes. I want to modify the script to add a File/Folder column
How can I add a column based on the "FILE_Type" value?
If FILE_Type = "" (Blank), the value should be "Folder" else it Should be file.

Or another option is that if FILE_Type is blank the value should be "Folder"

$dir = "C:_Files"

$results = @()

gci $dir -Recurse -Depth 0 | % {

$temp = [ordered]@{
    #NAME            = $_
    FILE_COUNT      = (gci -File $_.FullName -Recurse | measure | select -ExpandProperty Count)
    FOLDER_COUNT    = (gci -Directory $_.FullName -Recurse | measure | select -ExpandProperty Count)
    DIRECTORY_PATH  = $_.Fullname
    SIZE            = "{0:N2} MB" -f ((gci $_.Fullname -Recurse | measure -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB)
    FILE_Type        = $_.Extension
    Charchter_Count =  $_.fullname.length
    Created         =   $_.CreationTime
    Modified        = $_.LastWriteTime


}

$results += New-Object PSObject -Property $temp

}

$results | export-csv -Path "C:\Temp\Dept.csv" -NoTypeInformation

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2021-11-01T20:04:06.437+00:00

    The text of your post seems to be at odds with the code and the post's subject regarding the "FILE_Type" property! Your code says to get the file extension, but the the text asks for it to be either the string "Directory" of "File".

    See if this is any help:

    $dir = "C:_Files"
    
    Get-ChildItem $dir -Recurse -Depth 0 | 
        ForEach-Object {
            [PSCustomObject]@{
                NAME            = Split-Path $_.FullName -Leaf
                FILE_COUNT      = (gci -File $_.FullName -Recurse).count
                FOLDER_COUNT    = (gci -Directory $_.FullName -Recurse).count
                DIRECTORY_PATH  = $_.FullName | Split-Path -Parent
                SIZE            = "{0:N2} MB" -f ((gci $_.Fullname -Recurse | measure -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB)
                FILE_Type        = If ($_.PSIsContainer){"Directory"} else {"File"}
                Character_Count =  $_.fullname.length
                Created         =   $_.CreationTime
                Modified        = $_.LastWriteTime
            }
        } | export-csv -Path "C:\Junk\Dept.csv" -NoTypeInformation
    

    Why have you used the "-Recursive" switch and then set the depth to "0"????

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Albert Edelstein 91 Reputation points
    2021-11-03T21:23:18.613+00:00

    I modified your script: FILE_Type = If ($_.PSIsContainer){"Directory"} else {"File"}

    to: FILE_Type = (&{if($.Extension -eq ""){"Folder"} else {$.Extension}})

    Thank you

    0 comments No comments