简体   繁体   English

Powershell - 获取部分名称作为变量

[英]Powershell - Get partial name as Variable

First posting here, and I'm a beginner in Powershell (Or most script, in the matter of fact).第一次在这里发帖,我是 Powershell 的初学者(实际上是大多数脚本)。 What a try to do here is to take a part of a name file, and use it later To download a file from our server with the same name.这里尝试做的是获取一个名称文件的一部分,稍后使用它从我们的服务器下载同名文件。

$name = Get-childitem -Path "$env:USERPROFILE\AppData\Local\Microsoft\Outlook\*.ost" -name
$username = $name.Remove($name.IndexOf('@'))
Write-Output $Username

I only get the the file name without what I put between ${.}我只得到文件名,而没有我在 ${.} 之间放置的内容

Thanks for any help谢谢你的帮助

Split the string on the @ and grab only the first item from the resulting array:拆分@上的字符串并仅从结果数组中获取第一项:

$name = "FirstName.LastName@company.com - more text you don't care about.ost"

# String.Split() returns an array of strings, `[0]` gives us the first item in that array
$username = $name.Split('@')[0]

Alteratively, remove everything from the original string beginning at the first @ :或者,从第一个@开始的原始字符串中删除所有内容:

$username = $name.Remove($name.IndexOf('@'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM