Using windows command prompt in PowerShell - Network Drives - powershell

I'm having trouble moving from command prompt to powershell. Usually I hold the shift key while right clicking, select 'open command prompt here', and use dir /s>filename.txt to get a list of all files in a directory and its subfolders. However, my company just updated our computers and now I can't access the command prompt from the network folder I'm trying to get a directory list of.
When I right click and hold the shift key there is no option to 'open the command prompt' from the folder I'm selecting. I opened command prompt manually and attempted to navigate to the network folder, but no dice. I get an error regarding UNC (?) or that it basically can't do it because it's a network folder.
I attempted to use powershell the same way I use Command prompt given as an answer Here, however I get an error message
PS Microsoft.PowerShell.Core\FileSystem::\\my.company.com\companydata\MainProject\Records\Field Work\Invoices> dir /s >file.txt
dir : Cannot find path 's' because it does not exist.
At line:1 char:1
+ dir /s >file.txt
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (s:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
I also attempted to re-instate the command prompt option in the expanded right-click +shift key list as detailed here. However, I don't have the permissions required to run all the steps.
I just need a directory of everything in a specified network folder, just a list of all the files in the folder and subfolders: file paths, file names, and dates created. I used to be able to get it using command prompt but apparently, that's not available. I'm unsure how to use powershell, and help is much appreciated, in either getting new, simple, easy to understand powershell resources so I can teach myself, or in finding a work around so I can continue using command prompt.
Is there an interpretation guide I can use that goes from Command Prompt to Powershell, and also will let me list files in network drives? (I can sort of understand the current resources out there for having powershell list files in a directory on the computer, but what I need is the powershell to be able to list files in a specified network folder).

In powershell, dir is an alias for Get-ChildItem I believe you're getting that error because switches/arguments work differently in PowerShell. The first parameter by position is Path, so the system thinks you are providing the value /s for the path.
You can do this in PowerShell without having to launch cmd by leveraging Get-ChildItem. As you what the complete structure including the subfolders, user -Recurse
You could also export to csv instead of text file. There's a lot of data - Select can be used to restrict which fields you export.
$Path = "\\my.company.com\companydata\MainProject\Records\Field Work\Invoices"
$Files = Get-ChildItem -Path $Path -Recurse
$Files | Out-file file.txt
$Files | Select-Object FullName,CreationTime | Export-Csv file.csv -NoTypeInformation

Related

Copy file between remote file systems using windows PowerShell

I am on a corporate VPN. I have access to two remote file shares which have windows mapped paths. I can view both file shares in Windows Explorer. My goal is to copy a 1.8 gb .csv file from one share, to the other. I can copy much smaller files with no problem, simply using windows explorer. However, for files around 300mb or greater, I get an error copying the file. The file appears in the destination file share as the correct size, but reading it into python confirms that not all the csv rows are copied.
I am now attempting to copy the file using Windows 10 PowerShell, to open the door to more control over the copy operation, rather than the GUI copy command executed through the Windows 10 desktop environment. (I am replacing the true path names, with stand-ins for privacy)
When I run: Copy-Item \\sourcePath\aFile.csv \\destinationPath\aFile.csv -Verbose -Force
I get the error
Copy-Item : An unexpected network error occurred.
At line:1 char:1
+ Copy-Item '\\sourcePath\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
I am hoping that in powershell there is some way to either make the copy operation more robust to VPN bandwidth/connection stability limitations, or to cut my client computer out of the copy operation, and order the source file share to directly copy the file to the destination machine, without my machine serving as a middle man relay between the two. It is clear from my network traffic that my machine is downloading and then uploading the file while the failing copy operation runs.
Are you looking to replace the same item that's in the remote path?
Copy-Item -path \\sourcePath\aFile.csv -destination \\destinationPath\aFile.csv\ -Verbose -Force
It could very well be creds aren't being passed since youre on a VPN which you would have to double hop.
A solution is to use robocopy to copy the file.
robocopy “\\aSourcePath\\” “\\aDestinationPath\\” “aFileInSourcePath.something” /mt /z
runs until the file is transfered, is robust to VPN disruptions that seem to cause standard Windows Explorer copy/paste to fail.

Export-Csv - Access to the path 'C:\export.csv' is denied

I'm getting the information I need out of the line, although when I go to export the information, I get an error.
When I run this code:
get-process | Export-csv -Path "C:\export.csv" -NoTypeInformation
I get this error:
Export-csv : Access to the path 'C:\export.csv' is denied.
At line:1 char:14
+ get-process | Export-csv -Path "C:\export.csv" -NoTypeInformation
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How can I export to CSV?
You need to use an admin console or change the location of the csv file to C:\temp or something. The home drive require admin permissions.
EDIT:
The elevation requirement is only with system directories like:
C:\
C:\Windows
C:\Program Files
C:\Program Files (x86)
C:\Users
Notice that you can change C:\Users\<YourUserName> but not any other directories under C:\Users. Windows was built for multiple users even if you may be the only one using it. The above directories are common for all users and changing its contents would effectively change the Windows environment for other users too since windows rely on the integrity of these directories. Allowing a normal user to delete/modify/add files to these could have dangerous results for the OS and other people who use it.
So it tries to makes sure that only an Admin user is authorized to make changes to it. It is also the same reason why you get prompted for admin permissions when installing a software. It is installed to C:\Program Files or C:\Program Files (x86), make changes to the registry and is available to all users of that computer. Same with removing it. If a program does not require to do that, it doesn't require admin privileges. Eg: Portable softwares. Some programs only use the user directory or users temp directory C:\Users\<Username>\AppData\Local\Temp. Such programs are only available to the user that installed it.
If you have a custom directory like C:\myGames, adding files to it wont require elevation. So C:\Temp is just another directory like that. You will also notice that C:\ will let a normal user create a folder but not a file.
SUMMARY: Any changes that affect other user directories or the windows environment will require admin rights. Others don't.
In my case, the error occurred because I failed to specify the file name. The correct syntax is;
Export-Csv -path ".\targetList.csv"
and not -path "."

Powershell - shortened the directory prompt, but how to save the change?

Using Powershell in Windows 10. To change the prompt from:
PS C:\Users\b.HQ\Desktop\tsdev\my_folder>
PS my_folder> tsc
I used the following command in Powershell:
function prompt {'PS ' + $(Get-Location | Split-Path -Leaf) + ">"}
But, each time I restart Powershell, I have to reenter this. Is there any way to persist this change?
P.S. I know nothing about the config of Powershell, and I have looked for a solution, but apart from the prompt I am using, I did not see a way of saving it.
Run powershell as administrator, then run the following:
Test-Path $Profile
if it returns false then no you don’t have a profile file yet, so create it:
New-Item –Path $Profile –Type File –Force
(this will create profile file, or will overwrite the existing one)
Then, edit your profile file:
notepad $Profile
put your function in the file and save.
I created this neat prompt to that shows the drive and last folder.
For you example it would render as
PS C:\Users\b.HQ\Desktop\tsdev\my_folder>
as
PS C:\...\my_folder>
The prompt function is:
function prompt {"PS " + (get-location).drive.name+":\...\"+ $( ( get-item $pwd ).Name ) +">"}

Dealing with blank folder names while using get-childitem in powershell

I built a script, that searches through all directories recursively with Get-ChildItem. The problem is, there exist directories with blank names (done with alt+255).
When the script encounters such a directory, it still lists the files in this directory, but does not search in its sub-directories.
I don't think it is possible in powershell. but you can skip to cmd and use
cmd -c dir $Location /s
that works!
As #Bert Levrau mentioned above you can do a recursive search in CMD. Using Get-ChildItem in Powershell with a folder that has an ALT + 255 name will throw it into an infinite recursive loop. You can invoke a CMD process from Powershell though using the following example: $result = cmd /c $directoryPath /s
At that point, you can work through the result to find the information that you need.

Powershell Copy-item command not working in script although it does work when run from a command line

I am on a Windows 7 machine trying to execute a PowerShell script to copy a template directory to another directory. The command I am executing looks like:
Copy-Item -path "$projectsFolder$SourceFolder" -destination "$Test" -recurse -verbose;
The parameters are as follows:
path: C:\Users\username\Documents\Visual Studio 2010\Projects\TemplateSolution\Source
Destination: C:\Users\username\Documents\Visual Studio 2010\Projects\test\source\main
When I run this command at a PowerShell prompt, the files are copied correctly. If I try and execute the command in the script with verbose enabled, it appears to copy the files and directories, but only the top level directory is created in the file system. I am not sure why this would happen and I would appreciate any guidance or troubleshooting steps to perform.
Make sure you put quotes around the directory names if they have spaces in them. Also, you may need the -Force parameter to create destination directories if they do not exist.