1

As some background, this should take an excel file, and convert it to PDF (and place the PDF into a temporary folder).

E.g. 'C:\Users\gjacobs\Desktop\test\stock.xlsx' becomes 'C:\Users\gjacobs\Desktop\test\pdf_merge_tmp\stock.pdf'

However, the new file path does not return correctly.

If I echo the string $export_name from within the function, I can see that it has the correct value: "C:\Users\gjacobs\Desktop\test\pdf_merge_tmp\stock.pdf".

But once $export_name is returned, it has a different (incorrect value): "C:\Users\gjacobs\Desktop\test\pdf_merge_tmp C:\Users\gjacobs\Desktop\test\pdf_merge_tmp\stock.pdf".

function excel_topdf{

    param(
        $file
    )
    
    #Get the parent path
    $parent = Split-Path -Path $file

    #Get the filename (no ext)
    $leaf = (Get-Item $file).Basename

    #Add them together.
    $export_name = $parent + "\pdf_merge_tmp\" + $leaf + ".pdf"

    echo ($export_name) #prints without issue.

    #Create tmp dir
    New-Item -Path $parent -Name "pdf_merge_tmp" -ItemType "Directory" -Force

    $objExcel = New-Object -ComObject excel.application
    $objExcel.visible = $false

    $workbook = $objExcel.workbooks.open($file, 3)
    $workbook.Saved = $true

    $xlFixedFormat = “Microsoft.Office.Interop.Excel.xlFixedFormatType” -as [type]
    $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $export_name)
    $objExcel.Workbooks.close()
    
    $objExcel.Quit()

    return $export_name
}

$a = excel_topdf -file 'C:\Users\gjacobs\Desktop\test\stock.xlsx'
echo ($a)
4
  • 2
    New-Item returns the item it created, it gets concatenated with your return output. Assign it to a throwaway variable or otherwise discard it. Commented Jan 12, 2021 at 13:03
  • Yep, that fixed it. Thanks a lot. Can I ask why the behaviour of new-item is to concatenate to a return? Commented Jan 12, 2021 at 13:13
  • New-Item returns the object is has created. Commented Jan 12, 2021 at 13:33
  • It's not New-Item as such, anything returned by any cmdlet will be concatenated into your output, that's just how Powershell is designed. Commented Jan 13, 2021 at 10:24

1 Answer 1

1

The issue you're experiencing is caused by the way how PowerShell returns from functions. It's not something limited to New-Item cmdlet. Every cmdlet which returns anything would cause function output being altered with the value from that cmdlet.

As an example, let's take function with one cmdlet, which returns an object:

function a {
 Get-Item -Path .
}

$outputA = a
$outputA

#### RESULT ####

    Directory:


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d--hs-       12/01/2021     10:47                C:\

If you want to avoid that, these are most popular options (as pointed out by Lasse V. Karlsen in comments):

# Assignment to $null (or any other variable)
$null =  Get-Item -Path .

# Piping to Out-Null
Get-Item -Path . | Out-Null

NOTE: The behavior described above doesn't apply to Write-Host:

function b {
 Write-Host "bbbbbb"
}

$outputB = b
$outputB

# Nothing displayed

Interesting thread to check if you want to learn more.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.