1

I'm trying to do something very simple, but with being new to powershell I'm having trouble figuring out what is wrong:

PS C:\Windows\system32> $directoryMaker = New-Object [System.IO.Directory]::CreateDirectory("C:\test")
New-Object : Cannot find type [[System.IO.Directory]::CreateDirectory]: make sure the assembly containing this type is loaded.
At line:1 char:19
+ $directoryMaker = New-Object [System.IO.Directory]::CreateDirectory("C:\test")
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

1 Answer 1

2

try just:

$directoryMaker = [System.IO.Directory]::CreateDirectory("C:\test")

In this case you are using a static method ( CreateDirectory(string s) ) of the [System.IO.Directory] then you don't need to instantiate a [System.IO.Directory] object to reference the new folder created by this method itself.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot, I still haven't learned my way around powershell.
@inquisitor This is more c# then Powershell, the powershell way is: $directoryMaker = new-item -Path c:\test -type directory
Yeah I was experimenting with C# because I have to do some custom things with streams and wanted to make sure I understood how to do something more simple. Thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.