title: -NoProfile -ExecutionPolicy Bypass not working for Get-ChildItem
body: <p>The following script is being called from a batch file; everything works except for the 'Get-ChildItem'; which is the most important part. Not sure why I am able to create folder and files, but I can't loop through a directory. I have admin rights to the machine; I am using '-NoProfile -ExecutionPolicy Bypass' in the batch, and I also have full rights to the folders.</p>
  
  <p>The line that is giving me trouble is: $files = Get-ChildItem $uploadFilePath after it errors out, it throws the rest of the script in error.</p>
  
  <p>I am an amateur at PowerShell; and I managed to frankenstein this script together for multiple other examples; so please forgive me if you see anything that just looks out-right-wrong.</p>
  
  <p>Would appreciate any advice! </p>
  
  <p>Batch File:</p>
  
  <pre><code>set sninstancename=NAMEHERE
  set snsuthorization=AUTHERE
  set snstagingtable=TABLEHERE
  set snrecordlimit=100
  
  powershell -NoProfile -ExecutionPolicy Bypass -file "_sn_upload_files.ps1" -sninstancename "%sninstancename%" -snsuthorization "%snsuthorization%" -snstagingtable "%snstagingtable%" -snrecordlimit "%snrecordlimit%"
  </code></pre>
  
  <p>PS Script:</p>
  
  <pre><code>&lt;# Set global variables #&gt;
      param (
          [string]$sninstancename,
          [string]$snsuthorization,
          [string]$snstagingtable,
          [string]$snrecordlimit
      );
  
      $bulkUploadCount = 0;
      $createBulkRecord = "true";
      $bulkRecordSysID = "";
      $bulkRecordNumber = "";
      $logFileFolderName = Get-Date -format "yyyyMMdd";
      $logFolderPath = "$($PSScriptRoot)\$($logFileFolderName)";
      $uploadFilePath = "";
      $sninstanceurl = "https://$($sninstancename).service-now.com/";
      $snstagingurlpostfix = "api/now/table/$($snstagingtable)";
      $snuploadapipostfix = "ecc_queue.do?JSONv2&amp;sysparm_action=insert";
  
  function makelog-folder () {
      $logFolderExists = Test-Path "$logFolderPath";
  
      if ($logFolderExists -eq $False) {
          New-item "$logFolderPath" -type directory | Out-Null
      }
  }
  
  function welcome-prompt () {
      Write-host "`n";
      Write-Host "ServiceNow - PowerShell Bulk Upload Utility. Version 1.0";
      Write-host "`n";
      Write-host "Please Enter the full file-path for the files you wish to upload.";
      Write-host "Target files should not be in the same directory as PowerShell Script";
      Write-host "`n";
      $filePath = Read-Host -Prompt "Path";
      $filePathExists = Test-Path "$filePath";
  
      if ($filePathExists -eq $True) {
          return $filePath
      } else {
          Write-host "The file-path: $($filePath) is invalid. ";
          Write-host "Push any key to exit.";
          $exit = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");
          exit;
      }  
  
  }
  
  function Get-MimeType() { 
      param([parameter(Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullorEmpty()][System.IO.FileInfo]$CheckFile) 
      begin { 
          Add-Type -AssemblyName "System.Web"         
          [System.IO.FileInfo]$check_file = $CheckFile 
          [sting]$mime_type = $null 
      } 
      process { 
          if ($check_file.Exists) {  
              $mime_type = [System.Web.MimeMapping]::GetMimeMapping($check_file.FullName)  
          } 
          else { 
              $mime_type = "false" 
          } 
      } 
      end { return $mime_type } 
  }
  
  function create-staging-table(){
      if ($sninstancename -And $snsuthorization -And $snstagingtable) {
          $uri = "$($sninstanceurl)$($snstagingurlpostfix)";
          $headers = @{"Authorization"=$snsuthorization};
  
          try {
              $answer = Invoke-RestMethod -Uri "$($uri)" -ContentType "application/json" -Method Post -Headers $headers;
  
              if ($answer.result.number) {
                  $bulkRecordNumber = $answer.result.number;
                  $answer.result | ConvertTo-Json -depth 999 -Compress | Out-file "$($logFolderPath)\$($bulkRecordNumber).txt" -Encoding ASCII;
                  return $answer.result.sys_id;
              }
          } catch {
              &lt;#
              $errorhandle = @{"Message"="Error";"StatusCode"=$_.Exception.Response.StatusCode.value__;"Description"=$_.Exception.Response.StatusDescription;"File"=$fileName}
              $errorhandle | ConvertTo-Json -depth 999 -Compress | Out-file "$snlogfile" -Encoding ASCII -append
              #&gt;
              Write-host "error";
              Write-host "Push any key to exit.";
              $exit = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");
          }
      } else {
          Write-host "Missing parameters:";
          Write-host "Push any key to exit.";
          $exit = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");
          exit;
      }
  }
  
  function upload-files(){
      if ($sninstancename -And $snsuthorization -And $snstagingtable -And $snrecordlimit) {
  
         Write-Output "Transfering Files, please Wait...";
         $files = Get-ChildItem $uploadFilePath; 
  
         for ($i=0; $i -lt $files.Count; $i++) {
              $uri = "$($sninstanceurl)$($snuploadapipostfix)";
              $headers = @{"Authorization"=$snsuthorization};
  
              $fileContentEncoded = [convert]::ToBase64String((get-content "$($files[$i].FullName)" -encoding byte));
              $mimeType = $(Get-MimeType -CheckFile "$files[$i].FullName");
              $jsonbody = @{agent="AttachmentCreator";topic="AttachmentCreator";name="$($files[$i].BaseName)$($files[$i].Extension):$($mimeType)";source="$($snstagingtable):$($bulkRecordSysID)";payload=$fileContentEncoded} | ConvertTo-Json -Compress
  
              Write-host "Uploading $($files[$i].FullName)";
              Write-host "Please wait ...";
  
              try {
                  $answer = Invoke-RestMethod -Uri "$uri" -ContentType "application/json" -Method Post  -Headers $headers -Body $jsonbody;
  
                  if ($answer.records) {
                      $answer.result | ConvertTo-Json -depth 999 -Compress | Out-file "$($logFolderPath)\$($bulkRecordNumber).txt" -Encoding ASCII;
                      return $answer.result.sys_id;
                  }
              } catch {
                  #$errorhandle = @{"Message"="Error";"StatusCode"=$_.Exception.Response.StatusCode.value__;"Description"=$_.Exception.Response.StatusDescription;"File"=$fileName}
                  #$errorhandle | ConvertTo-Json -depth 999 -Compress | Out-file "$snlogfile" -Encoding ASCII -append
                  Write-host "error";
                  Write-host "Push any key to exit.";
                  $exit = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");
              }
          }
      } else {
          Write-host "Missing parameters:";
          Write-host "Push any key to exit.";
          $exit = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");
          exit;
      }
  }
  
  cls
  $(makelog-folder);
  $uploadFilePath = $(welcome-prompt);
  $bulkRecordSysID = $(create-staging-table);
  $(upload-files);
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Path: C:\Users\MYNAME\Desktop\Upload
  get-content : Access to the path
  'C:\Users\MYNAME\Desktop\Upload\Files are Here' is denied.
  At C:\Users\MYNAME\Desktop\PSPOCNEW\_sn_upload_files.ps1:107 char:62
  +             $fileContentEncoded = [convert]::ToBase64String((get-content
  "$($fil ...
  +
  ~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : PermissionDenied: (C:\Users\nick.g...\Files are
     Here:String) [Get-Content], UnauthorizedAccessException
      + FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsof
     t.PowerShell.Commands.GetContentCommand
  
  Exception calling "ToBase64String" with "1" argument(s): "Value cannot be null.
  Parameter name: inArray"
  At C:\Users\MYNAME\Desktop\PSPOCNEW\_sn_upload_files.ps1:107 char:77
  +             $fileContentEncoded = [convert]::ToBase64String((get-content
  "$($fil ...
  +
  ~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : ArgumentNullException
  
  Get-MimeType : Cannot process argument transformation on parameter
  'CheckFile'. Cannot convert value "Files are Here OLD File Upload  - Copy
  (152).pdf File Upload  - Copy (153).pdf File Upload  - Copy (154).pdf File
  Upload  - Copy (155).pdf File Upload  - Copy (156).pdf File Upload  - Copy
  (157).pdf File Upload  - Copy (158).pdf File Upload  - Copy (159).pdf File
  Upload  - Copy (160).pdf File Upload  - Copy (161).pdf File Upload  - Copy
  (162).pdf File Upload  - Copy (163).pdf File Upload  - Copy (164).pdf File
  Upload  - Copy (165).pdf File Upload  - Copy (166).pdf File Upload  - Copy
  (167).pdf File Upload  - Copy (168).pdf File Upload  - Copy (169).pdf File
  Upload  - Copy (170).pdf File Upload  - Copy (171).pdf File Upload  - Copy
  (172).pdf File Upload  - Copy (173).pdf File Upload  - Copy (174).pdf File
  Upload  - Copy (175).pdf File Upload  - Copy (176).pdf File Upload  - Copy
  (177).pdf File Upload  - Copy (178).pdf File Upload  - Copy (179).pdf File
  Upload  - Copy (180).pdf File Upload  - Copy (181).pdf File Upload  - Copy
  (182).pdf File Upload  - Copy (183).pdf File Upload  - Copy (184).pdf File
  Upload  - Copy (185).pdf File Upload  - Copy (186).pdf File Upload  - Copy
  (187).pdf File Upload  - Copy (188).pdf File Upload  - Copy (189).pdf File
  Upload  - Copy (190).pdf File Upload  - Copy (191).pdf File Upload  - Copy
  (192).pdf File Upload  - Copy (193).pdf File Upload  - Copy (194).pdf File
  Upload  - Copy (195).pdf File Upload  - Copy (196).pdf File Upload  - Copy
  (197).pdf File Upload  - Copy (198).pdf File Upload  - Copy (199).pdf File
  Upload  - Copy (200).pdf[0].FullName" to type "System.IO.FileInfo". Error:
  "The specified path, file name, or both are too long. The fully qualified file
  name must be less than 260 characters, and the directory name must be less
  than 248 characters."
  At C:\Users\MYNAME\Desktop\PSPOCNEW\_sn_upload_files.ps1:108 char:51
  +             $mimeType = $(Get-MimeType -CheckFile "$files[$i].FullName");
  +                                                   ~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidData: (:) [Get-MimeType], ParameterBindin
     gArgumentTransformationException
      + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-MimeTyp
     e
  </code></pre>