1

Fairly new to powershell.

I am trying to create two subfolder based on a user input variable. These subfolders are Bookmarks and Organisers

I can create the main folder based on this variable, however all subfolders I try and create go to the root F:\ drive directory instead of being nested.

See code below

Import-Module NTFSSecurity
Import-Module ActiveDirectory

$userloginname = Read-host -Prompt "Enter the users name"

$s2 = New-PSSession -ComputerName SERVERNAME
Invoke-Command -ScriptBlock {param($userloginname)New-Item -Name $userloginname -ItemType directory  -Path "f:\"} -ArgumentList $userloginname -Session $s2 **#this creates fine under F drive**
Invoke-Command -ScriptBlock {Set-Location  f:\$userloginname\} -Session $s2
Invoke-Command -ScriptBlock {param($userloginname)New-Item f:\$userloginname\Organisers -ItemType directory -force} -Session $s2 ##Doesnt nest under f:/$userloginname
Invoke-Command -ScriptBlock {param($userloginname)New-Item f:\$userloginname\Bookmarks -ItemType directory -force} -Session $s2 ##Doesnt nest under f:/$userloginname

I thought this would be fairly simple but no matter what combination or ordering it never nests under f:$userloginname

Is there something I am doing wrong?

1
  • 1
    In the first one you pass in the variable as an argument correctly using -ArgumentList. For the others you don't? Either pass in $userloginname the same way using -ArgumentList or change to $using:userloginname learn.microsoft.com/en-us/powershell/module/… Commented Aug 3, 2021 at 4:12

1 Answer 1

2

You're only passing $userloginname as argument on your first call to Invoke-Command and not on the next invocations. In addition, the code could be simplified to do everything on your first call to Invoke-Command:

Import-Module NTFSSecurity
Import-Module ActiveDirectory

$userloginname = Read-host -Prompt "Enter the users name"

$s2 = New-PSSession -ComputerName SERVERNAME

Invoke-Command -ScriptBlock {
    param($userloginname)

    $dir = Join-Path 'F:' -ChildPath $userloginname

    'Organisers','Bookmarks' | ForEach-Object {
        New-Item (Join-Path $dir -ChildPath $_) -ItemType Directory -Force
    }

} -ArgumentList $userloginname -Session $s2

Also, as Daniel pointed out in his comment, you can use $using:userloginname on a remote session which would simplified the code even more. See about Remote Variables.

$userloginname = Read-host -Prompt "Enter the users name"

$s2 = New-PSSession -ComputerName SERVERNAME

Invoke-Command -ScriptBlock {

    $dir = Join-Path 'F:' -ChildPath $using:userloginname

    'Organisers','Bookmarks' | ForEach-Object {
        New-Item (Join-Path $dir -ChildPath $_) -ItemType Directory -Force
    }

} -Session $s2

This should also work, which would make Invoke-Command not needed in this particular case:

$server = '\\SERVERNAME\F$'
$userloginname = Read-host -Prompt "Enter the users name"
$dir = Join-Path $server -ChildPath $userloginname

'Organisers','Bookmarks' | ForEach-Object {
    New-Item -Path (Join-Path $dir -ChildPath $_) -ItemType Directory -Force
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.