Search code examples
powershelltextpowershell-3.0powershell-4.0

How to copy content of one text file to another text file with only filename and its extension using PowerShell?


I have a text file which has list of files with their full path along with extensions, what i want is to copy this list of files to another text file only with their filename and extension and not the entire path.

For eg - Copying demo.txt content to test.txt

Demo.txt 

C:\Demo\Excluded Files\AppData\List\ConfigurationUtility.BackUp.exe.config
C:\Demo\Excluded Files\AppData\List\ConfigurationUtility.exe.Config
C:\Demo\Excluded Files\AppData\List\ConfigurationUtility.WMIExtensions.XML
C:\Demo\Excluded Files\AppData\List\Microsoft.Administration.dll
C:\Demo\Excluded Files\AppData\List\ModelConfiguration.xml
C:\Demo\Excluded Files\AppData\List\Newtons.Json.dll
C:\Demo\Excluded Files\AppData\List\Data\Help\List User Guide.pdf
C:\Demo\Excluded Files\AppData\List\Data\Help\ListHelp.chm
C:\Demo\Excluded Files\AppData\List\Data\Help\intel Load Balancing.pdf


test.txt

ConfigurationUtility.BackUp.exe.config
ConfigurationUtility.exe.Config
ConfigurationUtility.WMIExtensions.XML
Microsoft.Administration.dll
ModelConfiguration.xml
Newtons.Json.dll
List User Guide.pdf
ListHelp.chm
intell Load Balancing.pdf

How can i do that using PowerShell?


Solution

  • Get-Content demo.txt | %{Split-Path $_ -leaf} | Set-Content test.txt
    

    To answer the other question that you didn't ask in the first place (since I am already in the doghouse for providing code to posters that don't try first):

    Get-Content demo.txt | %{(Split-Path $_ -leaf).Split('.')[-1]} | Set-Content test.txt