Martin Wood
Martin Wood

Reputation: 77

passing string variable in New-item -path

I was writing this code to capture newly created AD's samaccountname and make a homefolder for it ! im facing this issue -

$ADServer= 'xyz'

$c = Get-EventLog Security -computername $ADServer -After (Get-Date).AddHours(-24) | Where-Object { $_.EventID -eq 4720 -and $_.Message -match "sam account name:\s+(.*)"} | ForEach-Object { $matches[1] } | Select-Object -First 1

New-Item -ItemType Directory -Path "\\abc\$c"

this is showing error - illegal characters in string, how can I create the folder of the same thing I captured in $c ?

Upvotes: 0

Views: 4105

Answers (1)

Avshalom
Avshalom

Reputation: 8889

Try this one:

New-Item -ItemType Directory -Path "\\abc\c$\$($c.Trim())"

or:

$c = $c.trim()
New-Item -ItemType Directory -Path "\\abc\$c"

The Reason: You have extra space letter at the end, this is why it fails,

Use the $c.trim() or $c -replace "\s" to remove the the space char

Upvotes: 1

Related Questions