Powershell function returning duplicate value

Steve Hanna 20 Reputation points
2025-10-30T15:19:12.9833333+00:00

I have a function to determine/create folders for archiving logs that returns the path of the directory to be used. It returns the calculated value twice. Why? Thanks in advance for your help.

C:\scripts\pospay\Archive\2025\10\30 C:\scripts\pospay\Archive\2025\10\30

Here's the function

function Get-Logfile-Directory {

param (

    [string]$RootDirectory

)

$Date = Get-Date

$Dir1 = $RootDirectory + "\" + $Date.Year.ToString("0000")

if (-not (Test-Path -Path $Dir1)) {

		New-Item -ItemType Directory -Path $Dir1

}

$Dir2 = $Dir1 + "\" + $Date.Month.ToString("00")

if (-not (Test-Path -Path $Dir2)) {

		New-Item -ItemType Directory -Path $Dir2

}

$Dir3 = $Dir2 + "\" + $Date.Day.ToString("00")

if (-not (Test-Path -Path $Dir3)) {

		New-Item -ItemType Directory -Path $Dir3

}

Return $Dir3

}

Here's the code calling the function

Write-Output "Archiving files"

$LogDirectory = Get-Logfile-Directory -RootDirectory $ArchiveDirectory

write-host $logdirectory

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Domic Vo 11,705 Reputation points Independent Advisor
    2025-10-30T16:01:17.75+00:00

    Dear Steve Hanna,

    The behavior you're observing—where the path appears twice like this:

    Code

    C:\scripts\pospay\Archive\2025\10\30
    C:\scripts\pospay\Archive\2025\10\30
    

    —likely results from a combination of how PowerShell handles output and how the function is being called.

    In your script:

    powershell

    $LogDirectory = Get-Logfile-Directory -RootDirectory $ArchiveDirectory
    write-host $logdirectory
    

    There are two key points to consider:

    1. Implicit Output from Function Call PowerShell automatically writes the return value of a function to the output stream if it's not being suppressed. So when you assign the result to $LogDirectory, the function still outputs the return value unless redirected or explicitly suppressed.
    2. Explicit Output via Write-Host You're also printing the same variable again with write-host $logdirectory. Note that PowerShell is case-insensitive, so $logdirectory and $LogDirectory refer to the same variable.

    To avoid duplicate output, you can suppress the implicit output by redirecting it or simply rely on one output method. For example:

    powershell

    $null = Get-Logfile-Directory -RootDirectory $ArchiveDirectory
    Write-Host "Log directory: $LogDirectory"
    

    Or, if you want to capture and display the result:

    powershell

    $LogDirectory = Get-Logfile-Directory -RootDirectory $ArchiveDirectory
    Write-Host "Log directory: $LogDirectory"
    

    This will ensure the path is printed only once.

    For logging or scripting purposes, consider using Write-Output or Out-File instead of Write-Host, as it provides more flexibility for redirection and automation.

    If this guidance proves helpful, feel free to click “Accept Answer” so we know we’re heading in the right direction 😊. And of course, I’m here if you need further clarification or support.

    T&B,

    Domic.


  2. MotoX80 37,156 Reputation points
    2025-10-31T12:19:01.01+00:00

    The New-Item cmdlet is returning a directory object which is getting added to the pipeline.

    User's image

    You can assign it to a variable like I did here with newdir. You don't need to use newdir for anything, you just need for the new-item output to go somewhere. You can also pipe it to out-null.

    If you want your function to return a string, then use "$newdir = " or " | out-null" and "Return $Dir3".

    If you want your function to return a DirectoryInfo object, then remove "Return $Dir3" and let the output of the New-Item calls to be returned.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.