Unwanted spaces in filepath concatenation inside a function

[string]$FileDS = Get-Date -Format "MMddyyyy"
[string]$outFile = $outpath+"\"+$computername+"_"+$FileDS+".csv"
New-Item -ItemType file $outfile -Force
$object | export-csv -path $outfile

This is a section of a function I’m using for a software inventory I’m running against remote machines. As it stands, the output adds unnecessary spaces between each value returned in these variables, and throwing the following error:

New-Item : Could not find a part of the path 'filepath \ prod-adcweb1 _ 20180209 .csv'.
At filepath\GetInstalledApplication.ps1:181 char:1
+ New-Item -ItemType file $outfile -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\users\username_ 20180209 .csv:String) [New-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand

export-csv : Could not find a part of the path 'filepath \ prod-adcweb1 _ 20180209 .csv'.
At filepath\GetInstalledApplication.ps1:182 char:11
+ $object | export-csv -path $outfile
+           ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Export-Csv], DirectoryNotFoundException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

I’ve been staring at this too long, wondering if someone else has an answer as to how I can remove these spaces. Thank you in advance.

Did you consider using Join-Path? Give this a try:

[string]$FileDS = Get-Date -Format “_yyyyMMdd”
[string]$outFile = = Join-Path -Path $outpath -ChildPath ($computername + $FileDS + ‘.csv’)

I’m getting the exact same error despite specifically calling for no spacing, seen here:

[string]$FileDS = Get-Date -Format "_yyyyMMdd"
[string]$outFile = Join-Path -Path $outpath -ChildPath ($computername + $FileDS + '.csv')
New-Item -ItemType file $outfile -Force
$object | export-csv -path $outfile

Error:

New-Item : Could not find a part of the path 'C:\filepath \ prod-adcweb1 _ 20180209 .csv'.
At C:\filepath\GetInstalledApplication.ps1:181 char:1
+ New-Item -ItemType file $outfile -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\Users\username..._ 20180209 .csv:String) [New-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand

export-csv : Could not find a part of the path 'C:\filepath \ prod-adcweb1 _ 20180209 .csv'.
At C:\filepath\GetInstalledApplication.ps1:182 char:11
+ $object | export-csv -path $outfile
+           ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Export-Csv], DirectoryNotFoundException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

So it seems to me that the cause for this error is in your $outpath. Where/how do you get this?

The function is Get-InstalledApplication, borrowed from elsewherenon the web. $outpath is a parameter of the cmdlet.

?? I meant how you fill in the variable $Outpath? You have to have an assignment somewhere in your code.

Sorry, stepped away for a few.

For reference, this is the code I’m modifying to suit my needs:

The only place outpath is filled is when I run the cmdlet, seen here:

Get-InstalledApplication -computername computername -outputtype CSV -outpath C:\filepath\filename.csv

OK. Even if I think that there’s a lot of room for improvemnt in this code - if you change the initial param block to the following it works at least:

Param(
[Parameter(Mandatory=$true)]
[string]$Computername,
[String]$OutputType,
[string]$outpath
)