none
Powershell Error The term 'Merge-CSVFiles' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct an d try again. RRS feed

  • Question

  • When put the code below in sindows powershell ise (x86), select all and choose "Run Selection (F8)" I am getting error: "Error: The term 'Merge-CSVFiles' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

    If I choose "Run Script (F5)" I don't get any error and I am getting my report, the same is happening if I select all code again and run with "Run Selection (F8)" (second time, no error).

    So only the first time when running it with F8 I am getting error....Any idea...anybody...please...

    Here is my code and complete error on the end: 

    Merge-CSVFiles MyFile*.csv -CSVPath C:\MyReps -XLOutput C:\MyReps\MyFileReport.xlsx

    Function Merge-CSVFiles
    {
    Param(
    $CSVPath = "C:\MyReps", ## Soruce CSV Folder
    $XLOutput="C:\MyReps\MyFileReport.xlsx" ## Output file name
    )

    $csvFiles = Get-ChildItem ("$CSVPath\*") -Include *.csv
    $Excel = New-Object -ComObject excel.application 
    $Excel.visible = $false
    $Excel.sheetsInNewWorkbook = $csvFiles.Count
    $workbooks = $excel.Workbooks.Add()
    $CSVSheet = 1

    Foreach ($CSV in $Csvfiles)

    {
    $worksheets = $workbooks.worksheets
    $CSVFullPath = $CSV.FullName
    $SheetName = ($CSV.name -split "\.")[0]
    $worksheet = $worksheets.Item($CSVSheet)
    $worksheet.Name = $SheetName
    $TxtConnector = ("TEXT;" + $CSVFullPath)
    $CellRef = $worksheet.Range("A1")
    $Connector = $worksheet.QueryTables.add($TxtConnector,$CellRef)
    $worksheet.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True
    $worksheet.QueryTables.item($Connector.name).TextFileParseType  = 1
    $worksheet.QueryTables.item($Connector.name).Refresh()
    $worksheet.QueryTables.item($Connector.name).delete()
    $worksheet.UsedRange.EntireColumn.AutoFit()
    $CSVSheet++

    }

    $workbooks.SaveAs($XLOutput,51)
    $workbooks.Saved = $true
    $workbooks.Close()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbooks) | Out-Null
    $excel.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()

    }

    Error: The term 'Merge-CSVFiles' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct an
    d try again.
    At line:1 char:15
    + Merge-CSVFiles <<<<  MyFile*.csv -CSVPath C:\MyReps -XLOutput C:\MyReps\MyFileReport.xlsx
        + CategoryInfo          : ObjectNotFound: (Merge-CSVFiles:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Friday, June 29, 2018 11:03 PM

Answers

All replies