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

Albert Edelstein 86 Reputation points

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.
4,627 questions
No comments
{count} votes

Accepted answer
  1. Rich Matheisen 35,191 Reputation points

    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 {
                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"????

    No comments

1 additional answer

Sort by: Most helpful
  1. Albert Edelstein 86 Reputation points

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

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

    Thank you

    No comments