Split-Path returns "System.Windows.Forms.TextBox, Text: mypath" 所以我不能将位置设置为该路径

Split-Path returns "System.Windows.Forms.TextBox, Text: mypath" so I cannot set-location as that path

我有一个带有 GUI 的脚本,它会弹出 Windows 资源管理器并允许用户选择一个文件来导出数据。但是,我想在他们选择的目录中操作一些文件,但是当我尝试在文件名上使用 Split-Path 时,出现此错误:

Set-Location : Cannot find drive. A drive with the name 'System.Windows.Forms.TextBox, Text' does not exist.

有没有办法删除返回文本的开头部分,只获取里面的路径文本?

//users selected path from GUI Note* this is the path but it is selected by the gui button through file explorer
$UsersPath = "C:\username\desktop\MyFolder\myFile.csv"

$newPath = Split-Path -Path "$UsersPath"

Set-Location "$newPath.Text"

//In this case, $newPath = System.Windows.Forms.TextBox, Text: C:\Users\username\Desktop\myFolder

我试过使用 .Text.ToString 都无济于事。

下面是我的代码,它让我打开一个对话框,要求用户选择一个文件。

function open_CSV_File{

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $openFileDialog.InitialDirectory = "C:\";
    $OpenFileDialog.Filter = "csv files (*.csv)|*.csv"
    "
    if ($OpenFileDialog.ShowDialog() -eq "OK"){
        $textbox_BrowseForCSV.Text = $OpenFileDialog.FileName 
    }
}

改变这个:

Set-Location "$newPath.Text"

为此:

Set-Location "$($newPath.Text)"