Hide powershell output - powershell

I have the following script:
param([Parameter(Mandatory=$true)][string]$dest)
New-Item -force -path "$dest\1\" -itemtype directory
New-Item -force -path "$dest\2\" -itemtype directory
New-Item -force -path "$dest\3\" -itemtype directory
Copy-Item -path "C:\Development\1\bin\Debug\*" -destination "$dest\1\" -container -recurse -force
Copy-Item -path "C:\Development\2\bin\Debug\*" -destination "$dest\2\" -container -recurse -force
Copy-Item -path "C:\Development\3\bin\Debug\*" -destination "$dest\3\" -container -recurse -force
The script takes a string and copies all files and folders from the static origin path to the given root string, amending some folders for structure clarity.
It works fine but prints out the results from the "New-Item" commands and I would like to hide that. I've looked at the net and other questions on SE but no definitive answers to my problem were found.
In case someone is wondering - I am using "New-item" at the beginning in order to circumvent a flaw in PS' -recurse parameter not copying all subfolders correctly if the destination folder does not exist. (I.e. they are mandatory)

Option 1: Pipe it to Out-Null
New-Item -Path c:\temp\foo -ItemType Directory | Out-Null
Test-Path c:\temp\foo
Option 2: assign to $null (faster than option 1)
$null = New-Item -Path c:\temp\foo -ItemType Directory
Test-Path c:\temp\foo
Option 3: cast to [void] (also faster than option 1)
[void](New-Item -Path c:\temp\foo -ItemType Directory)
Test-Path c:\temp\foo
See also: What's the better (cleaner) way to ignore output in PowerShell?

Related

Copy-Item when destination folder exists or doesn't

When destination folder exists the right way of copying files is defined here
Copy-Item 'C:\Source\*' 'C:\Destination' -Recurse -Force
If destination folder doesn't exist, some files in the source subfolders are copied straight into destination, without keeping original folder structure.
Is there a way to have a single Copy-Item command to address both cases and persist folder structure? Or is it too much to ask from Powershell?
You may want to use a if statement with test-path
this is the script i used to fix this problem
$ValidPath = Test-Path -Path c:\temp
If ($ValidPath -eq $False){
New-Item -Path "c:\temp" -ItemType directory
Copy-Item -Path "c:\temp" -Destination "c:\temp2" -force
}
Else {
Copy-Item -Path "c:\temp" -Destination "c:\temp2" -force
}

How to create symbolic link for non-existing folder?

I'd like to create link to a folder on desktop of remote computer. I do not have permissions to execute scripts on that computer, but I can copy files to that computer.
My idea was to create link to folder on local computer and then copy the link to remote computer.
But, I am getting error New-Item : Cannot find path 'C:\SomeFolder' because it
does not exist.
Here is my command:
New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value "c:\SomeFolder" -Force
Any ideas for workaround?
Try adding the -force parameter:
New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value "c:\SomeFolder" -force
You can do using mklink also . Make sure that the destination folder is available . You can use the Test-Path to check that :
$destination = "c:\SomeFolder"
if(Test-Path $destination)
{
cmd /c mklink "c:\Users\pocherka\Desktop\link" $destination
# OR you can use the new-item also. Just commented in the below line
# New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value $destination
}
else
{
New-Item $destination -ItemType Directory -Force
cmd /c mklink "c:\Users\pocherka\Desktop\link" $destination
}
Hope it helps

Simple inbuilt way to Create or Delete and Create A directory

In powershell scripts is there a single inbuilt way to recreate a directory, I tend to use this over and over in a lot of different scripts, while it does the job, it causes clutter, I can make a function, but I'd end up copy pasting it a lot, just wondering if there is a more straightforward way to do this.
If(Test-path $destination)
{
Remove-item $destination -Force -Recurse
}
New-Item $destination -type directory
No, there is no way to delete and create a directory in a single command in Powershell. Unless resorting to the mentioned function or module approach, it'll be 2 lines minimum.
You could achieve the same with:
Remove-item $destination -Force -Recurse -ErrorAction SilentlyContinue
New-Item $destination -type directory
Making it an inline function:
Function RecreateDirectory( [string]$destination ) {
Remove-item $destination -Force -Recurse -ErrorAction SilentlyContinue
New-Item $destination -type directory
}
RecreateDirectory("C:\OneDir")
RecreateDirectory("C:\AnotherDir")

How to prevent creating additional folder if destination folder exists while copying file using powershell

I am trying to copy a folder from the local computer to a remote server. It works but if the destination folder already exists it is creating a duplicate folder inside it.
copy-item -Path C:\test -Destination \\server\F$\testpassed -recurse -Force
To copy only the files from within C:\test to the \\server\F$\testpassed folder you need to use the following command:
Copy-Item -Path C:\test\* -Destination \\server\F$\testpassed -Recurse
\* is a wildcard for anything within the folder, and will cause Copy-Item to copy anything within the folder to the Destination. You could also use *.txt to only copy txt files if you wanted only a specific file type to be copied.
EDIT:
I would test for the presence of $TARGETDIR and then create it if needed. This way you only have a single copy command.
$TargetDir = "\\server\F$\testpassed"
$SourceDir = "C:\test"
if(!(Test-Path -Path $TARGETDIR)) {New-Item -Path $TARGETDIR -ItemType Directory}
Copy-Item -Path "$SourceDir\*" -Destination $TARGETDIR -Recurse
Using source path in below way will solve your issue
Copy-Item -Path C:\test*
Try
$Source = Get-childitem C:\test -Recurse
copy-item -Path $Source.FullName -Destination C:\temp -recurse -Force
Use GC to stop getting the folder as well as the contents.

How do I rename/move items and override even if they exist (for both files, folders and links)?

Is there one command to move items (or just rename as both source and destination are in one folder) with forcing overwrite that does work for any item types (leaf, container)?
Background: I am writing a script that replaces all hardlinks and junctions with respective relative symlinks. Sample code:
mkdir C:\Temp\foo -ErrorAction SilentlyContinue
'example' > C:\Temp\foo\bar.txt
cd C:\Temp
New-Item -ItemType Junction -Name bar -Target C:\Temp\foo
New-Item -ItemType SymbolicLink -Name bar2 -Target '.\foo'
# Produces error: Rename-Item : Cannot create a file when that file already exists.
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar' -force
# Unexpected behaviour: moves bar2 inside bar
Move-item -Path 'C:\Temp\bar2' -destination 'C:\Temp\bar' -force
# This works as per https://github.com/PowerShell/PowerShell/issues/621
[IO.Directory]::Delete('C:\Temp\bar')
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar'
I think you're looking for the UI experience of having the option to overwrite files and merge directories. These are just sophisticated error handling mechanisms to account for the same errors that you are seeing, thanks to Microsoft's thoughtful engineers.
mkdir C:\Temp\foo -ErrorAction SilentlyContinue
'example' > C:\Temp\foo\bar.txt
cd C:\Temp
New-Item -ItemType Junction -Name bar -Target C:\Temp\foo
New-Item -ItemType SymbolicLink -Name bar2 -Target '.\foo'
# Produces error: Rename-Item : Cannot create a file when that file already exists.
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar' -force
This makes sense. You have two distinct objects, so they cannot have the same identifier. It would be like trying to point to two different objects
# Unexpected behaviour: moves bar2 inside bar
Move-item -Path 'C:\Temp\bar2' -destination 'C:\Temp\bar' -force
This is not unexpected. When you specify a directory for the destination, it treats it as the target directory which the moved item should be placed inside.
# This works as per https://github.com/PowerShell/PowerShell/issues/621
[IO.Directory]::Delete('C:\Temp\bar')
Rename-Item -Path 'C:\Temp\bar2' -newName 'bar'
This is essentially what the thoughtful Microsoft engineers have done for you through their UI for merging folders and overwriting files.
Note that this is the same behavior for the .NET method in System.IO as well