如何获取 PowerShell 脚本的文件系统位置? [英] How can I get the file system location of a PowerShell script?
本文介绍了如何获取 PowerShell 脚本的文件系统位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个位于 D: emp
的 PowerShell 脚本.
I have a PowerShell script located at D: emp
.
运行此脚本时,我希望列出文件的当前位置.我该怎么做?
When I run this script, I want the current location of the file to be listed. How do I do this?
例如,此代码将在 DOS 批处理文件中完成;我正在尝试将其转换为 PowerShell 脚本...
For example, this code would accomplish it in a DOS batch file; I am trying to convert this to a PowerShell script...
FOR /f "usebackq tokens=*" %%a IN ('%0') DO SET this_cmds_dir=%%~dpa
CD /d "%this_cmds_dir%"
推荐答案
PowerShell 3+
运行脚本的路径为:
$PSCommandPath
它的目录是:
$PSScriptRoot
PowerShell 2
运行脚本的路径为:
$MyInvocation.MyCommand.Path
它的目录是:
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
这篇关于如何获取 PowerShell 脚本的文件系统位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文