locked
New-Item after New-Partition and Format-Volume RRS feed

  • Question

  • New to PowerShell and I couldn't find anything searching, so I apologize for any ignorance.

    I'm trying to automate creating a formatted disk partition and populating a base directory structure. Thus I do:

    New-Partition -DiskNumber 0 -UseMaximumSize -DriveLetter D
    
    Format-Volume -DriveLetter D -FileSystem NTFS -NewFileSystemLabel Data
    
    Update-Disk -Number 0

    And this works. I can create directories easily enough by doing:

    New-Item -Path "D:\" -Name "Data" -ItemType "directory"

    But if I chain both parts together, New-Item fails because "D:" is an invalid path. If I open a new PowerShell instance, it works. If I create the folders manually though Explorer, it works. If I call:

    Test-Path -Path "D:\"

    ...it returns False in the PowerShell instance that creates the partitions, but True in the second PowerShell instance.

    I am guessing that the first PowerShell instance has paths cached somewhere. I was hoping that Update-Disk would refresh this, but it doesn't seem to have any affect. How do I make the first PowerShell instance cognizant of the drive created by Format-Volume?

    Thank you,

    Jason Stern 


    Friday, June 22, 2018 9:03 PM

Answers

All replies