0

I am using PowerShell and BMC Control-M for automation, I have created a PowerShell script to create a folder:

$directoryname= "D:\sysdba" 
$DoesFolderExist = Test-Path $directoryname 
$null = if (!$DoesFolderExist){MKDIR "$directoryname"}

$directoryname= "D:\temp" 
$DoesFolderExist = Test-Path $directoryname 
$null = if (!$DoesFolderExist){MKDIR "$directoryname"}

I am using below command to create folder on host server:

<commands>
  <command>\\Path\SPUpgrade\Create_Folder.ps1</command>
</commands>

But it is creating a file instead of folder:

enter image description here

Any idea why? I am confused as why not creating folder and why file

TylerH
  • 20,799
  • 66
  • 75
  • 101
deepti
  • 729
  • 4
  • 17
  • 38
  • No clue why mkdir doesn't work anymore but you could try to replace it with: `new-item -Path $directoryname -ItemType Directory` – Martin Brandl Oct 12 '17 at 07:05
  • I cannot reproduce this behaviour with your code. Can you confirm that it is really your script producing the `sysdba` file and not some other process that runs at the same time? You can test this by changing the path from 'D:\sysdba' to 'D:\foo' and see if the same happens. – Manuel Batsching Oct 12 '17 at 07:16
  • It could be an alias? try md or suggestions provided here. – VGSandz Oct 12 '17 at 07:26
  • i checked new-itemtype directory same file is getting generated instead of folder – deepti Oct 12 '17 at 09:38

1 Answers1

1

Using mkdir from Powershell is not encouraged since mkdir is an external utility and not an internal Powershell command. Instead, use New-Item -ItemType directory to achieve what you want:

$directoryname= "D:\sysdba" 
if(!(Test-Path -Path $directoryname )){
    New-Item -ItemType directory -Path $directoryname
    Write-Host "created a new folder"
}
else
{
  Write-Host "The folder is already exists"
}

And you can do the same for "D:\temp".

Megabeets
  • 1,378
  • 11
  • 19