简体   繁体   中英

Powershell - Get partial name as Variable

First posting here, and I'm a beginner in Powershell (Or most script, in the matter of fact). 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('@'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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