我怎样才能得到一个PowerShell脚本的文件系统中的位置? [英] How can I get the file system location of a PowerShell script?

查看:215
本文介绍了我怎样才能得到一个PowerShell脚本的文件系统中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有位于 D PowerShell脚本:\\ TEMP

当我运行此脚本,我想被列出的文件的当前位置。我该怎么做呢?

例如,这code将完成它在DOS批处理文件;我试图将其转换为一个PowerShell脚本...

  FOR / F令牌有usebackq = *%%一个IN('%0')DO SET this_cmds_dir = %%〜DPA
CD / D%this_cmds_dir%


解决方案

的PowerShell 3 +

运行脚本的路径是:

  $ PSCommandPath

其目录是:

  $ PSScriptRoot

2的PowerShell

运行脚本的路径是:

  $ MyInvocation.MyCommand.Path

其目录是:

  $ PSScriptRoot =拆分路径$ MyInvocation.MyCommand.Path -Parent

I have a PowerShell script located at D:\temp.

When I run this script, I want the current location of the file to be listed. How do I do this?

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+

The path of a running scripts is:

$PSCommandPath

Its directory is:

$PSScriptRoot

PowerShell 2

The path of a running scripts is:

$MyInvocation.MyCommand.Path

Its directory is:

$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent

这篇关于我怎样才能得到一个PowerShell脚本的文件系统中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆