1

This is what I tried (C:\temp really exists):

$fso = New-Object -ComObject Scripting.FileSystemObject 
$ts = $fso.CreateTextFile("c:\temp:AAAAA.txt")      
$ts.Close() 

No error returned. The file is created somewhere (but not in c:\temp... if I put something inside, I can later read it) but I cannot find it.

Could anyone explain me why this code not produce an error & where the file gone?

braX
  • 11,506
  • 5
  • 20
  • 33

1 Answers1

2

The reason you don't get an error is probably that the call succeeded!

When you call CreateTextFile and pass the file path C:\temp:AAAAA.txt, you're actually asking to create a file C:\temp with an alternate data stream (an ADS) named AAAAA.txt.

You can see for yourself with Get-Item:

PS C:\> Get-Item C:\temp -Stream AAAAA.txt

PSPath        : Microsoft.PowerShell.Core\FileSystem::C:\temp:AAAAA.txt
PSParentPath  : Microsoft.PowerShell.Core\FileSystem::C:\
PSChildName   : temp:AAAAA.txt
PSDrive       : C
PSProvider    : Microsoft.PowerShell.Core\FileSystem
PSIsContainer : False
FileName      : C:\temp
Stream        : AAAAA.txt
Length        : 0

You can also read and write to alternate data streams (requires PowerShell version 5 or newer):

PS C:\> "Hello there!" |Set-Content C:\temp -Stream AAAAA.txt
PS C:\> Get-Content C:\temp -Stream AAAAA.txt
Hello there!

Please beware that these data streams are intrinsic to NTFS and won't persist if you copy a file to a non-NTFS file system or upload them to a website for example.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206